Is there a way I can invoke a helper inside a helper in handlebars? I would like to do something like this:
{{#each myCustomHelper}}
<li>{{this}}</li>
{{/each}}
Where the invocation of myCustomHelper returns an array of strings.
I'm trying to do this from node using express-handlebars and I do not want to pass that array of strings on each res.render.
I would like to avoid also outputting html from myCustomHelper.
Is the above possible?
Thanks!
And found the answer. At least in my case the syntax will be:
{{#each (myCustomHelper)}}
<li>{{this}}</li>
{{/each}}
Had to dig into this issue to find the correct syntax
Related
I'm trying to build a Python Unit Test File Template in PyCharm. The overall result I want to achieve is:
A user creates a new file with my template, say "widget_builder.py"
Inside the template I want to create the class name by taking the file name "widget_builder" and turning it into "WidgetBuilderTests"
It looks like I need to use a Live Template to manipulate the file template variable $FILE_NAME$?
How can I create a Live Template that given a passed in variable (in this case $FILE_NAME$), applies both the underscoresToCamelCase and capitalize functions to it?
If I declare the Template text as:
$CLASS_NAME$
...and then edit variables, how can I reference a passed in variable of '$FILE_NAME$'?
I'd imagine it to look something like this, but I just can't get it to work:
I'm sure there must be a way to do this, but I just can't quite wrap my head round it.
Is this possible? Thanks!
EDIT
I've got a bit further. If I define the template as this:
If I then use it, this happens:
So the end result of $CLASS_NAME$ (WidgetBuilder) on the left is what I want, but I don't want $FILE_NAME$ (widget_builder) to be there once I hit return.
So your problem here is that $FILE_NAME$ is not a native variable in the live templates, merely an arbitrary name. What you actually want to be using is another function: fileNameWithoutExtension().
So your template would look something like:
I would like to assign variables in an mvel template.
Assuming that my template has only a property foo defined, I would like to do the following:
#{bar=foo}
#{bar.name}
Unfortunately this outputs me the toString() of foo and then the property name of bar. I would like to do the same but without printing anything with the assignation.
(just in case anyone lands here...)
According to this documentation, you can use #code blocks to execute statements without emitting output.
#code{bar=foo}
#{bar.name}
HAML and ractive.js seem to play well together (if you don't mind not indenting the contents of a mustache section), though I have found one problem I can't solve.
When I do this:
.like{ class: "{{#if like}}active{{/if}}" }
...
I get this:
<div class='like like}}active{{/if}} {{#if'>...</div>
It appears that the HAML parser is assuming that word order doesn't matter inside of a class declaration, and is messing with my string (though I can't imagine why), but in this case I need that string to be preserved!
I know I could use plain html, but it gets quite messy when there are many nested tags.
Any ideas?
I figured it out...
If I change it from this:
.like{ class: "{{#if like}}active{{/if}}" }
to this:
%div{ class: "like{{#if like}} active{{/if}}" }
It works fine.
Dojo's dijit html5 tags use an attribte name data-dojo-props. The value is basically a JSON string without quotes around the property names and without the outermost braces.
It looks something like this.
data-dojo-props="prop1:'xyz', prop2:true, prop3: { subprop1: 1, subprop2: 'abc'}"
I'm using C# to write this out from a C# object using JSON.NET and passing in the object pointer. I found settings to leave out the property name quotes, but I can't figure out a graceful way to remove the outside braces.
For now, I'll run the string through a regex to remove them, but was wondering if someone new a better way.
I serialize each top level property separately and make it a global javascript variable. I then reference that variable in data-dojo-props. I admit it's not that elegant.
My concern with your approach above, is if the value of subprop2 contains a quote, you will get a parser error.
<script type="text/javascript">
menuData = {THE SERIALIZED JSON GOES HERE};
</script>
<div data-dojo-type="SomeWidget" data-dojo-props="menuData: menuData"></div>
I need to use some handlebars block helpers inside of an element's attribute. How can I get
.some.class{:class => "{{if property}} otherClass {{/if}}"}
to parse properly? Here is the compiled output as is:
<div class='class otherClass property}} some {{/if}} {{if'></div>
I've tried escaping every non-word character, trying single quotes, and adjusting the spacing, but nothing seems to fix it. I'm hoping to find a more elegant solution than just using the :plain keyword.
I've found that you need to add some character before the {{}} for Haml to accept it. For example:
%div.some.class{:class => "a{{foo}}"}
Not idea, but suites my purposes.
Actually, the trick doesn't work for anything handlebars expression that is moderately complex.
In the end I resolved this by using string literals in haml. So in the example case shown in the question. The below haml is needed, remember to close your tag, which is the last line.
\ <div class='class {{if property}} otherClass {{/if}} some '>
-# Normal haml goes here and must not start at the same indent as the above literal
\ </div>
Move all the classes in to the class attribute instead of using both the class shorthand dot notation and a class attribute.
.col-md-12.card{ class: "{{toLowerCase level_name}}-master-card" }
becomes
%div{ class: "col-md-12 card {{toLowerCase level_name}}-master-card" }
HAML 4.0.7