am I right in thinking you can't access a variable from outside the loop whilst looping?
For example:
{loop $nav_header}
<li><img src="{$template.imagefolder}/{$icon}" width="48" height="48" border="0" alt="{$title}" /><br />{$title}</li>
{/loop}
where I'm trying to use $template.imagefolder (as an example) from outside the loop?
Yours,
Chris
(Copying my answer from http://forum.dwoo.org/viewtopic.php?id=617 for future reference)
Well, you can, but you've to know what you want exactly..
$nav_header is an array so you can access it from outside the loop by doing {$nav_header.0.template.imagefolder} for example, that'll give you what you want for the first item of the array.
If you want to access a top level variable from within the loop, i.e. if you got $path and $nav_header in your main data node, you'd do {$_.path}, which is the equivalent of {$_parent.path}
The other approach, if you don't know where to look exactly in your array, is to save a variable while you're looping, and then you can access it from out of it, however you can't assign to the parent scope at the moment, so you should use foreach for that, since it doesn't move the scope, i.e. :
{foreach $nav_header elem}
<li><img src="{$elem.template.imagefolder}/{$elem.icon}" width="48" height="48" border="0" alt="{$elem.title}" /><br />{$elem.title}</li>
{if $elem.title == "foo"}{$folder = $elem.template.imagefolder}{/if}
{/foreach}
{$folder}
Hope this helps.
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}}
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.
I'm trying to read some values from the XML file which I created, but it gives me the following error:
coldfusion.runtime.UndefinedElementException: Element MYXML.UPLOAD is undefined in XMLDOC.
Here is my code
<cffile action="read" file="#expandPath("./config.xml")#" variable="configuration" />
<cfset xmldoc = XmlParse(configuration) />
<div class="row"><cfoutput>#xmldoc.myxml.upload-file.size#</cfoutput></div>
Here is my config.xml
<myxml>
<upload-file>
<size>15</size>
<accepted-format>pdf</accepted-format>
</upload-file>
</myxml>
Can someone help me to figure out what is the error?
When I am printing the entire variable as <div class="row"><cfoutput>#xmldoc#</cfoutput></div> it is showing the values as
15 pdf
The problem is the hyphen - contained in the <upload-file> name within your XML. If you are in control of the XML contents the easiest fix will be to not use hyphens in your field names. If you cannot control the XML contents then you will need to do more to get around this issue.
Ben Nadel has a pretty good blog article in the topic - Accessing XML Nodes Having Names That Contain Dashes In ColdFusion
From that article:
To get ColdFusion to see the dash as part of the node name, we have to "escape" it, for lack of a better term. To do so, we either have to use array notation and define the node name as a quoted string; or, we have to use xmlSearch() where we can deal directly with the underlying document object model.
He goes on to give examples. As he states in that article, you can either quote the node name to access the data. Like...
<div class="row">
<cfoutput>#xmldoc.myxml["upload-file"].size#</cfoutput>
</div>
Or you can use the xmlSearch() function to parse the data for you. Note that this will return an array of the data. Like...
<cfset xmlarray = xmlSearch(xmldoc,"/myxml/upload-file/")>
<div class="row">
<cfoutput>#xmlarray[1].size#</cfoutput>
</div>
Both of these examples will output 15.
I created a gist for you to see these examples as well.
I have been trying to getting to drips with django, and I am experimenting with the tutorials. So, what I have at the moment is a View class which spits out a queryset - which I access in the template as follows:
<a id="{{i.0}}" .....##whatever goes here
which works great. What I would like to do is to prepend the character x before the value of {{i.o}} - but I am unsure how to do this. So, I am able to append using:
<a id="{{i.0|add:"x"}}" .....##whatever goes here
but, how do I prepend?
Any guidance would be great on this.
<a id="x{{ i.0 }}" .....##whatever goes here
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.