Is there a method to pass a variable to a L&L template via the template shortcode (like passing variables to standard shortcodes)? I am trying to pass a post_id to template to then use in the loop parameters within the template.
Thanks!
Is there a method to pass a variable to a L&L template via the template shortcode (like passing variables to standard shortcodes)? I am trying to pass a post_id to template to then use in the loop parameters within the template.
Thanks!
Hello, thank you for the question.
Currently there’s no direct way to pass variables to a template via the shortcode - but that does sound useful. Probably the simplest way to support this is by making shortcode attributes available in the template as variables. Something like:
[template id=1 post_id=123]
…which could be used from the template like:
<Get post_id />
…or more practically:
<Loop type=post id="{Get post_id}">
...
</Loop>
OK, I’ve made a note to implement this, and will let you know when it’s ready.
Awesome. And that is exactly what I was referring to and fits my use case exactly.
Thanks!
Hello - In the newest version, the Template tag (and shortcode) passes its attributes as “local variables” to the template.
Local variables is a new feature also, briefly documented here: https://loop.tangible.one/tags/get#local-variable
Basically, they exist only within a template post (or file) - so they don’t affect other templates that use the same variable names. It works with the Get
and Set
tag, using the local
attribute.
So, if the shortcode is:
[template id=1 post_id=123]
…the variable can be used like:
<Get local=post_id />
<Loop type=post id="{Get local=post_id}">
I’d love to hear if that works for you.
Hello,
Using an attribute in the [Template]
shortcode for the first time, it works fine for me.
Very handy feature to use the same template in various situations by varying a value.
It would worth to add it to the doc:
https://docs.loopsandlogic.com/82/template?category_id=62#shortcode
@avanti this functionality is already documented on that page you linked to, but I just added a short sentence to the shortcode section to remind people to read the paragraph just above it about local variables. Let me know if you think anything about that page is unclear and I’d be happy to rework it.
Hi @benjamin,
Yes true, and thanks for having added a reminder in the Shortcode part:
It would be like a repetition, but maybe adding a shortcode + variable example would make the thing even more obvious for impatient readers:
[Template id=123 my_variable=some_value]
Following this thread, i’m trying to evaluate the shortcode variable in a Switch condition, it always returns the same result:
The template:
region: <Get local=region />
<Switch variable=region>
<When value="bayOfPlenty" />
150
<When value="wellington" />
20
<When />
0
</Switch>
2 shortcodes with different variable values:
[template name=my-shortcode region=bayOfPlenty] projects
[template name=my-shortcode region=wellington] projects
region: <Get local=region />
returns the variable values alright but then the 1st condition is always returned:
region: bayOfPlenty 150
region: wellington 150
It should be:
region: bayOfPlenty 150
region: wellington 10
I also tried:
<Switch region>
<Switch field="region">
I’m a bit lost, any idea please?
Hi @avanti! The Switch
tag works with the same core conditions as the If
tag, so you might want to take a look at those here. In your case, you’ve used <Switch region="{Get local=region}">
but region
is not a valid condition. You could try using check
in this case. Using variable
makes sense, but I think that only works with regular variables, not local variables, so that’s why it doesn’t work in this instance. Using field
wouldn’t work since you’re not working with data from a field. Hope that helps!
Hi @benjamin,
Thanks for your help.
Sorry, i’m lost, i have changed my initial code above in the meantime… i will follow your leads.
Here’s my latest code:
region: <Get local=region /><br>
<If variable="region" exists>
variable exists<br>
<Else />
variable doesn't exist<br>
</If>
<Switch check="{Get local=region}">
<When is value=bayOfPlenty />
152
<When is value=wellington />
95
<When />
0
</Switch>
It returns:
region: bayOfPlenty
variable doesn’t exist
152
region: wellington
variable doesn’t exist
0
At least 2 different values, but not correct for wellington
check
was a good advice, but weird that it works with If
and not with Switch
:
<Get local=region /><br>
<If check="{Get local=region}" is value="bayOfPlenty">
152
<Else if check="{Get local=region}" is value="wellington" />
95
<Else />
0
</If><br>
<Switch check="{Get local=region}">
<When is value="bayOfPlenty" />
152
<When is value="wellington" />
95
<When />
0
</Switch>
Output:
bayOfPlenty
152
152
wellington
95
0
I use to transfer a local into a variable. Then all of this works fine.
<Set myvar><Get local=region /></Set> <Switch variable=myvar> <When value="bayOfPlenty" /> ....
Thank you @PolarRacing, it works indeed with a transitional “regular variable” as said @benjamin (which the local variable passed in the shortcode is not).
I thought of it and tried but wasn’t able to make it work, my L&L syntax wasn’t correct.
So, thanks much for bringing an example!
Here’s my code now:
<Get local=region /><br>
<Set the_region><Get local=region /></Set>
<Switch variable=the_region>
<When value="bayOfPlenty" />
152
<When value="wellington" />
95
<When />
0
</Switch>
The output:
bayOfPlenty
152
wellington
95
And, beyond this topic but that was the final goal, the complete template to return dynamically the total number of posts for a given Taxonomy term passed in the shortcode:
<Set the_region><Get local=region /></Set>
<Switch variable=the_region>
<When value="bayOfPlenty" />
<Loop type=project taxonomy=region terms=bay-of-plenty></Loop>
<When value="wellington" />
<Loop type=project taxonomy=region terms=wellington></Loop>
<When />
Not a region
</Switch>
<Get loop=previous_total /> Projects
The shortcode:
[template name=my-shortcode region=bayOfPlenty] Posts
The output:
152 Posts
I think there might be an issue with Switch
and When
when you specify a comparison. It seems that when you don’t specify a comparison (which should use is
as the comparison by default anyway, which is what you’re trying to do), everything works, as you’ve shown in your last example here.
Setting another variable within your template shouldn’t be necessary since you can just use check="{Get local=region}"
. The only reason your other template above wasn’t working seems to be because of the is
in <When is value="wellington" />
. If you get rid of that and just write <When value="wellington" />
, it seems to work as you’d expect. So I think the most efficient markup to do what you’re trying to do would be something like this since it avoids setting a second variable unnecessarily:
<Switch check="{Get local=region}">
<When value="bayOfPlenty" />
<Loop type=project taxonomy=region terms=bay-of-plenty></Loop>
<When value="wellington" />
<Loop type=project taxonomy=region terms=wellington></Loop>
<When />
Not a region
</Switch>
<Get loop=previous_total /> Projects
I’ve started documenting this for the devs so we’ll look into why Switch
and When
seems not to work when you specify a comparison or try using one other than is
. I’m not sure exactly what the issue is here but I’ll report back here with more info.