I have an Aurelia application up and running and I am using a template with bindable parameters.
<template bindable="textitems">
${textitems}
<section id="one" class="wrapper">
<div class="inner flex flex-3">
<div class="flex-item left">
<div repeat.for="textitem of textitems">
<p>${textitem}</p>
</div>
</div>
</div>
</section>
</template>
And I am passing it in like this
<attractor textitems="${attractors}"></attractor> where attractors is the array of items.
This basically is not behaving as I would like for it to.
${textitems} is spitting out the right content in this case one,two but - when it gets to the repeat for section - aurelia complains that it (textitems) is not iterable.
I have since found out that this is because it becomes a string of the array being output. So it becomes 'one,two' rather than ['one','two']
If so there must be a better way for me to pass this data down into the template.
Bindable definately seems the cleanest method, but Id love to be proven wrong.
Thanks for your time, no one seems to have been presented with this issue yet, but I am just getting started, and I think it will help others.
I needed to add .bind to the bindable section in the original template call.
textitems="${attractors}" - Caused the passed value to be the array rendered as a string.
textitems="attractors" - Caused the passed value to literally be the word attractors
textitems.bind="attractors" - Caused the passed value to be the array - as intended.
(suggested by Jesse de Bruijne)
One can presume that Aurelia needs .bind to handle the item as the object it is and not a string rendering of it. The ${} syntax causes the template to spit out the text rendering of whatever the thing is and just putting the name of the variable just spits out the name itself.
Related
This question already has an answer here:
Handlebars.js: index access to global.json from a loop
(1 answer)
Closed 2 years ago.
I am trying to get a property of an object in handlebars dynamically dependent upon the pid that the #each loop is currently on. I have tried the following code but the value isn't getting populated at all. I'm imagining there's some sort of context problem, but as I'm quite new to handlebars, I don't know the way to go about doing this.
<div class="card-deck">
{{#each entries}}
{{! backend only hidden comments}}
<div class="card">
<img class="card-img-top img-fluid" src="/userimgres/{{pid}}/0.jpg" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><strong>{{name}}</strong></h5>
<p class="card-text">{{desc}}</p>
</div>
<div class="card-footer">
<small class="text-muted">Posted {{../ago.[pid]}} ago (ID: {{pid}})</small>
</div>
</div>
{{/each}}
</div>
My ago object is setup with the key as the pid and the value as a string describing, in human readable format, how long it's been since a specified date/time.
For what it's worth, it's generated by a separate function from a constant object, hence why it's not in the same object to begin with. It looks like this:
{
'1000': '2 hours ago',
'1001': 'a month ago',
'1002': '3 days ago',
'2000': '4 days ago',
'2001': '12 days ago',
'2002': '4 months ago',
'2003': 'a month ago'
}
When I change the code to be static and query {{../ago.[1001]}}, I receive my string for that specific ID. Since the code relies on a for each, however, this obviously returns the same exact string for the other entries.
How can I modify this statement to dynamically query the ago object for a key's value - the key being defined by the pid that is currently being iterated through in the #each (i.e. what I'm doing elsewhere in this codeblock by simply calling {{pid}})?
I have tried {{../ago.[{{pid}}]}} and several other arrangements but none seem to work.
Many thanks in advance for any help, and I'm happy to clear anything up if this is unclear.
You must use the lookup helper as documented here
From the docs:
The lookup helper allows for dynamic parameter resolution using Handlebars variables.
This is useful for resolving values for array indexes.
{{#each people}}
{{.}} lives in {{lookup ../cities #index}}
{{/each}}```
In the instance of this question, you can simply change this example from using the current index to use a variable (in this case pid) located in the same context that the #each loop is in.
{{lookup ../ago pid}} should work for you. It gets the object ago from the parent context, then based off the #each loop's current iteration of entries.pid, it gets the value from ../pid for the key of pid it's on.
In other situations, you may need to continue reading the documentation linked in this answer for other ways of using the locate helper:
It can also be used to lookup properties of object based on data from the input. The following is a more complex example that uses lookup in a sub-expression to change the evaluation context to another object based on a property-value.
{{#each persons as | person |}}
{{name}} lives in {{#with (lookup ../cities [resident-in])~}}
{{name}} ({{country}})
{{/with}}
{{/each}}
There are two hbs files, one is under another layer, for example:
testA.hbs contains,
<div>
{{/testB.hbs}}
</div>
<div id="area">
Hello, world
</div>
At testB.js, I want to call the id area which is presented at testA.hbs. How can I achieve this?
Based on my understanding on what you need, you want to pass the property id from one template test-a to another template test-b.
So in order to make a property id available to your template test-b, you must pass it in like this {{test-b id="area"}}
Now you can access the property id in your
test-b.hbs as {{id}}
test-b.js as this.get('id')
Have a look at my ember-twiddle for a working example. Replicated the same scenario with two components.
I have a structure to invoke my component in this way, in a .hbs file
{{#myComponent as |section|}}
{{#if (eq section "section1")}}
this is the content for the first section <br>
{{else if (eq section "section2")}}
Some content for section two <br>
More content for second section <br>
{{/if}}
{{/myComponent}}
I want to be able to pass some parameters along, like this
{{#myComponent as |section| param1="xyz" param2=true}}
It is resulting in a parse error while build.
How can i still pass parameters to be accessed by the component in this scenario?
Thanks in advance!!
Figured that the entire component object has to be used, and then the "as" key word follows. Like this
{{#myComponent param1="xyz" param2=true as |section|}}
I've been playing around with golang for a couple of weeks now, and recently started to look into the revel framework.
In other languages/frameworks that I've used, it's always been possible to check what the current route is from within the template. Something that made it easier for me to keep things like navigation in a separate template file, and just do this:
<!-- in header template -->
<ul class="nav">
{{ if eq .req.action "App.Index" }}
<li class="active"><a href="#">
{{ else }}
<li><a href="{{url "App.Index" }}">
{{end}}
Home</a></li>
<!-- other links here -->
</ul>
This isn't the actual code I'm writing, but I hope this makes it clear what the idea is: Have a template for the navigation handy, and set classes/links according to which action is the current one.
After some time going through the source code, and a number of golang template examples, I couldn't quite see any way in which the current action or anything else is exposed to the template.
To get around this, I'm currently using a func interceptor which automatically sets a render argument for me:
func prependRoute(c *revel.Controller) revel.Result {
c.RenderArgs["_current_route"] = c.Action
return nil
}
This is working fine, but it feels a bit hacky. I think I'm probably missing something really obvious that would allow me to reserve func interceptors for when I really need them.
The question, then, is simple: What is the correct, and most reliable way to find out what the current action is in a revel template?
I am having a great deal of difficulty getting my head round displaying secveral resources on one page with Ditto. I cant seem to get TV's to show along with my content.
Heres how I have set it out:
I have a page with my Ditto call:
[!Ditto? &parents='134' &orderBy='createdon ASC' &tpl='temp'!]
I have a simple chunk called temp set up as such:
<div id="content">
[*articlename*]
[+content+]
</div>
And I have a template with the TV articlename assigned to all the resource under parent 134.
The content shows fine but none of the TV's do. Can anyone point me in the right direction? thanks!
I think the problem is in your syntax. You need to use a placeholder tag in the chunk for your TV:
Try this:
<div id="content"> [+articlename+] [+content+] </div>
I have found the answer: You are meant to use [+articlename+] for 'chunk TVs' rather then [*articlename*]. This is different to getResources.