I have this Play template, dynamicLink.scala.html...
#( urlWithQuotes: Html, id: Html, toClick: Html )
#uniqueId_With_Quotes() = {
Html("\"" + (#id) + "_" + scala.util.Random.nextInt.toString + "\"")
}
#defining(uniqueId_With_Quotes()) { uniqueID =>
<a id=#uniqueID class="dynamicLink" href=#urlWithQuotes> #toClick </a>
<script><!--Do stuff with dynamic link using jQuery--></script>
}
It generates a special link with some Javascript. I render this link like so...
#dynamicLink(
Html("#{routes.Controller.action()}"),
Html("MyID"),
Html("Click Me")
)
When I render it, I get...
<a id=
Html("\"" + (MyID) + "_" + scala.util.Random.nextInt.toString + "\"")
class="dynamicLink" href=#{routes.Controler.action()}> Click Me </a>
This is not what I want to render. I want to render this...
<a id="MyID_31734697" class="dynamicLink" href="/path/to/controller/action"> Click Me </a>
How do I make this HTML escape correctly?
* Take #2 - replacing Html params with String *
#(urlWithQuotes: String, id: String, toClickOn: String)
#uniqueId_With_Quotes() = {
Html("\"" + (#id) + "_" + scala.util.Random.nextInt.toString + "\"")
}
#defining(uniqueId_With_Quotes) { uniqueID =>
<a id=#uniqueID class="dynamicLink" href=#urlWithQuotes> #toClickOn </a>
...
}
With...
#dynamicLink2(
"#{routes.Controller.action()}",
"MyID",
"Click Me"
)
Renders...
<a id=
Html("\"" + (MyID) + "_" + scala.util.Random.nextInt.toString + "\"")
class="dynamicLink" href=#{routes.Controller.action()}> Click Me </a>
<script>
...
</script>
* Changing Html to String didn't work *
* Note that " #uniqueId_With_Quotes() " expands into " Html("\"" + (MyID) + "_" + scala.util.Random.nextInt.toString + "\"") ". I want it to actually execute string concatenation. *
Also, this should be obvious, but I want each link and the accompanying jquery to be rendered with an ID that is unique to that link and I don't want the controller to have to worry about assigning these unique id's. My way of doing that is by appending a random number to each id (although it might just be better for the view to have a count). I need to have this stateful behavior in the view because I need "dynamicLink" to be totally transparent to the controller.
Did you tried to use the variables as String types?
#( urlWithQuotes: String, id: String, toClick: String )
I find the solution. You have to pass a Call object.
#dynamicLink(
({routes.Controller.action()}),
"MyID",
"Click Me"
)
Pass those parameters into...
#(urlNoQuotes: Call, id: String = "", toClickOn: String = "")
#uniqueId_With_Quotes() = #{
Html("\"" + (id) + "_" + scala.util.Random.nextInt.toString + "\"")
}
#url() = #{
Html("\"" + urlNoQuotes + "\"")
}
#defining( url() ) { processedURL =>
#defining(uniqueId_With_Quotes()) { uniqueID =>
...
}
}
^ Now it works.
Related
$(document).ready(function() {
$.ajax({
url: 'https://dev.azure.com/{org}/infosoftaz/_apis/wit/workitems/1?api-version=7.0',
type:"GET",
dataType: 'json',
headers: {
'Authorization': 'Basic' + btoa("" + ":" + '{PAT}')
},
}).done(function( results ) {
console.log( results );
result = JSON.stringify(results);
var div = "";
div += "<p>" + result + "</p>";
div += "<p class='description'>" + result + "</p>";
$('#data_fetch').html(div);
});
});
<div id="data_fetch"></div>
Column A
Column B
Cell 1
Cell 2
Cell 3
Cell 4
According to your description, you can refer to this answer and then extract the result in html table.
Use:
JSON.parse(results)
Please check if it meets your requirements.
For example, native Magento theme-frontend-blank\web\js\navigation-menu.js has such code:
$.widget('mage.navigationMenu', {
options: {
...
collapsableDropdownTemplate:
'<script type="text/x-magento-template">' +
'<li class="level0 level-top more parent">' +
'<div class="submenu">' +
'<ul><%= elems %></ul>' +
'</div>' +
'</li>' +
'</script>'
And then they do
this.collapsableDropdown = $(
mageTemplate(
this.options.collapsableDropdownTemplate,
{elems: this.elemsToCollapseClone}
)
);
Is it possible to do something like this
collapsableDropdownTemplate: getTemplate(some_relative_path_to_template) ?
instead of
collapsableDropdownTemplate:
'<script type="text/x-magento-template">' +
'<li class="level0 level-top more parent">' +
'<div class="submenu">' +
'<ul><%= elems %></ul>' +
'</div>' +
'</li>' +
'</script>'
My template is a little bit bigger and I want to allow frontend developers to modify it.
I have setup my posts in Hexo and assigned tags to each post. However the title tag is not capitalizing the way I would like.
This is the rendered HTML:
<title>Viewing pizza | My site</title>
But I would to achieve this:
<title>Viewing Pizza | My site</title>
The tag: pizza is lowercase, and not sure how to make the tag begin with a capital letter within the title tag (e.g. Pizza, Pasta, Italy and so on).
My code:
<%
function capitalize (str) { return str.charAt(0).toUpperCase() + str.substring(1).toLowerCase() }
var title = page.title;
if (is_archive()) {
title = capitalize(__('Viewing'));
if (is_month()) {
title += ': ' + page.year + '/' + page.month;
} else if (is_year()) {
title += ': ' + page.year;
}
} else if (is_category()) {
title = capitalize(__('Viewing')) + ': ' + page.category;
} else if (is_tag()) {
title = capitalize(__('Viewing')) + ': ' + page.tag;
}
%>
<title><% if (title) { %><%= title %> | <% } %><%= config.title %></title>
Thanks in advance!
Here is a function to capitalize each words of a sentence :
function capWords(str) {
// we split string by words in an array
// and we iterate on each word to capitalize the first letter
// and we join each element with a space
return str.split(' ').map(function(str) {
return str[0].toUpperCase() + str.substr(1).toLowerCase()
}).join(' ');
}
In your code :
<%
function capWords(str) {
// we split string by words in an array
// and we iterate on each word to capitalize the first letter
// and we join each element with a space
return str.split(' ').map(function(str) {
return str[0].toUpperCase() + str.substr(1).toLowerCase()
}).join(' ');
}
var title = page.title;
if (is_archive()) {
title = __('Viewing');
if (is_month()) {
title += ': ' + page.year + '/' + page.month;
} else if (is_year()) {
title += ': ' + page.year;
}
} else if (is_category()) {
title = __('Viewing') + ': ' + page.category;
} else if (is_tag()) {
title = __('Viewing') + ': ' + page.tag;
}
%>
<title>
<% if (title) { %>
<%= capWords(title) + ' | ' %>
<% } %>
<%= config.title %>
</title>
I don't know if this is new in hexo, but if you're still looking, there is titlecase, which is a function you can use in your templates.
This should come with your hexo installation now. If you are using ejs as your renderer, you can use it as the documentation states:
You could do <%- titlecase('pizza') %> and get what you want.
Should you need to write your own functions, the preferred way is to write them in a /scripts/my_helpers.js file (name the .js file anything you want, but it has to be in scripts in your project directory). Alternatively, publish your module prefixed with hexo- and import it into your project (make sure it's listed in package.json if you're doing this).
You can then make your javascript function available with the following incantation in your .js file:
// note, I haven't tested this code.
hexo.extend.helper.register('capitalize', (aString) => {
return aString.split(" ").map(function(word) {
return word[0].toUpperCase() + word.substring(1).toLowerCase()}).join(" ")
});
Then you can use <%- capitalize("i am a strInG") %>
<title>
<% if (page.title) { %>
<%= capitalize(page.title) %> |
<% } %>
<%= config.title %>
</title>
`
I know it's a beginner question and probably very easy but I can't get it to work. I have a map with 2 utfgrids which show an information about bicycle and hiking routes. It is based on this example:
http://bl.ocks.org/milkbread/6449317
It works fine but I would like to change it this way. When hoovering the existing infobox should show up but when clicking on the route the popup should show up in the place where I clicked (with the same information that shows up in the infobox).
I tried different ways to apply it but I always get an error. Here's a link to my working webiste and a part of the code responsible for the infobox window.
http://wojtas82.zrujnowane.pl/utf2.html#15/54.5027/18.4886/osm-rowerowe-piesze
And the code:
utfGrid2.on('mouseover', function(e){ info.update(e);}).on('mouseout', function(e){ info.update();})
utfGrid1.on('mouseover', function(e){ info.update(e);}).on('mouseout', function(e){ info.update();})
var info = L.control();
info.options.position = 'bottomleft';
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
this.update();
return this._div;
};
info.update = function (props) {
this._div.innerHTML = (props ?
'<img src="img/' + props.data.route +'_'+ props.data.osmc_color +'_'+ props.data.osmc_background +'_'+ props.data.osmc_foreground + '.png" ><br /> ' + "<b>" + props.data.name + "</b><br />" + 'Długość: ' + (props.data.distance ? props.data.distance : '') + ' km' + "<br />" + 'Opis szlaku: ' + (props.data.description ? props.data.description : '') : '');
};
I will be very greatful for help.
Thank you very much.
Have you tried using
L.popup for your popup? Something like
utfGrid1.on('click', function(e) {
if (e.data) {
L.popup()
.setLatLng(e.latlng)
.setContent(e ? '<img src="img/' + e.data.route + '_' + e.data.osmc_color + '_' + e.data.osmc_background + '_' + e.data.osmc_foreground + '.png" ><br /> ' + "<b>" + e.data.name + "</b><br />" + 'Długość: ' + (e.data.distance ? e.data.distance : '') + ' km' + "<br />" + 'Opis szlaku: ' + (e.data.description ? e.data.description : '')).openOn(map);
}
});
I am trying to manually call the markdownify filter in a Jekyll plugin. Here is what I have:
module Jekyll
class ColumnBlock < Liquid::Block
include Jekyll::Filters
def initialize(tag_name, markup, tokens)
super
#col = markup
end
def render(context)
text = super
'<div class="col-md-' + #col + '">' + markdownify(text) + '</div>'
end
end
end
Liquid::Template.register_tag('column', Jekyll::ColumnBlock)
I get the following error: Liquid Exception: undefined method 'registers' for nil:NilClass
I am very new to Jekyll and Ruby. What do I have to include when I want to use the markdownify filter?
Why do not call directly the converter??
See the source code
def render(context)
text = super
site = context.registers[:site]
converter = site.getConverterImpl(Jekyll::Converters::Markdown)
'<div class="col-md-' + #col + '">' + converter.convert(text) + '</div>'
end
Update - getConverterImpl is deprecated in Jekyll 3, you should use find_converter_instance instead :
def render(context)
text = super
site = context.registers[:site]
converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
'<div class="col-md-' + #col + '">' + converter.convert(text) + '</div>'