14
votes
Accepted
Pass variable into include
You can leave the with lightswitchTest part off altogether, and all the variables from the parent template will be available inside the include too.
with expects an array, where the keys correspond ...
5
votes
Include a template that sets variables (global template variables)
Using macro command you can replace the set command, avoiding the disadvantage of long code every time you need to call a variable. I had the same need, and my solution is:
Parent template "main....
4
votes
Accepted
Use pure PHP and HTML instead of Templates
You're really fighting against the way Craft CMS does things.
I'd start by having a good read through the Craft Docs first, there really isn't a shortcut. The docs are really great though, and ...
3
votes
Accepted
Passing a variable to an include
You set the variable that your are passing in to your include to a string. And in your include you try to access it like an object.
The variable which you’re passing in to your include is set to a ...
3
votes
Accepted
Passing data to includes - overrides & defaults
There is no reason why your code should work, because any variable declared outside of your include file in the with {...} block will be immediately overwritten by your variable definition (#set) in ...
3
votes
How to include twig in the editor? e.g. Shortcodes for dynamic content
The typical 'Craft' way of doing this would be via a Matrix field instead of a single text block field, so that editors can sandwich the dynamic content (which would be its own block type) in between ...
3
votes
Accepted
Twig function to load templates into variable?
You can't do it exactly like that in Twig. But there're different approaches that'd do the same thing depending on exactly what you're trying to achieve.
For instance, you could parse the template ...
3
votes
Accepted
Using a variable that was set in a previous include
So what you're running into is Twig scoping rules, which is discussed at length in the article Twig Processing Order & Scope
The TL;DR is that a variable is only available in the context in which ...
3
votes
Use value of a dropdown field for an `include`
You don't need to (more specifically, you can't) nest Twig tags within other Twig tags. Ditch the inner Twig tag and its respective quotes, and just pass the variable directly into the outer Twig tag.....
3
votes
Accepted
search parameter passed to include
Your syntax is a bit off – the search() parameter doesn't accept multiple parameters, only a search query (e.g. .search('body:foobar')) or an object containing the search query plus search term ...
2
votes
Merging options to pass with an include
I suspect this isn't so much a problem with the strategy as the implementation of the MatrixBlockModel, which is the object type you're trying to merge with a standard Twig "object" or associative ...
2
votes
Accepted
Use variable passed via include to set value of paramenter
You'll need to concatenate the string you pass to the level parameter, using the ~ operator:
{% set pages = craft.entries.section('navigation').level('<='~navLevel) %}
2
votes
Accepted
Template Errors with Include on Single Entry
In your current code, the entry variable is actually an instance of ElementCriteriaModel, which is probably the reason you're getting errors.
In order to set entry to an actual EntryModel, you'll ...
2
votes
Include with -- variable does not exist error
You're probably looking for:
{% include "singlepage" with { 'currentPage': 'leaders' } %}
The key (the part before the :) needs to be a string, hence in quotes.
The relevant Twig template docs are ...
2
votes
Re-using includes when the object type changes (entry vs block) - what is the best method?
From what I can see here, the only important object you care about in the include file is the video object. So what you could do is simply passing the video field in parameter directly, using, for the ...
2
votes
Accepted
How to slightly alter _layout for one specific page?
Try putting it inside of a simple block as the default value...
{% block contactButton %}
{% include '_contact-button' %}
{% endblock %}
Then on your one-off page, override that block with ...
2
votes
Accepted
Passing optional variable values into an include template
So in this case with ctaUrl, the issue is that you are checking if ctaUrl is defined.
As long as a field exists, is defined will return true as in reality, the variable block.ctaUrl exists, it's ...
2
votes
How to include twig in the editor? e.g. Shortcodes for dynamic content
There's also the Shortcodes plugin
2
votes
Accepted
Setting body ID from includes does not work
So, I had a similar problem... Had a few things that didn't work for me, but I got it to work. First item: putting the set bodyID inside the {% block main %} didn't work for me, it had to be outside ...
2
votes
Setting body ID from includes does not work
So Twig has a particular processing order for things like variables, includes, extends and all that sort of stuff. Basically, you can't set a variable in an included template and then use that in an ...
2
votes
How to pass a chunk of twig from an 'include' statement?
What you're trying to do is possible using "variable variables" (i.e., a Twig object with a dynamically named sub-property), however in my opinion a better approach would be to use embed ...
1
vote
Accepted
Passing a variable to an include to set an image transform throws Setting Unknown Property error
Dropdown fields don't return the selected option as a string. Instead, they return a SingleOptionFieldData object that you can use to access the value of the selected option as well as its label. The ...
1
vote
Encoding special characters even with the raw filter due to include
As mentioned in the Discord you are probably looking for the source function.
The reason the include doesn't work is covered in the old PR for (what eventually became) source. It's probably best I ...
1
vote
Using {% cache %} tags around {% template %} includes (or in Header)
General answer: It is not wrong to put include tags in cache tags.
1
vote
Accepted
Passing variables into twig components
I forgot to post the solution:
_hero-banner.twig
{% set defaults = {
heroTitle : options.title is defined ? options.title : 'Title not set',
titleColor : options.color is defined ? options....
1
vote
Get the variables of a loop from an include file
Can't you fetch all the blocks with block.topicTeaserItems.all()
{% include 'modules/module-squareteaser/_module-squareteaser' with {
squareTeasers: block.topicTeaserItems.all()
} %}
And then you ...
1
vote
How do I pass variables to loops within includes?
You can't reference a variable (photo) created inside the partial loop outside the partial (and loop) – it doesn't exist yet – so what you're trying to achieve isn't possible.
What you could do, is ...
1
vote
Re-using includes when the object type changes (entry vs block) - what is the best method?
Perhaps a better method is to include; {% set object = block ?? entry %} at the top of the _includes/matrix/video.htm template and this negates the need to use with { object : entry } in the includes
1
vote
Set Parent Template Defined Content to Output in Child Template
You could use include...with, as specified in the Twig Docs here
Example for your case:
{# in Template C #}
{% include 'template-d.twig' with {'foo':'Hello World'} %}
This should give Template D ...
1
vote
Accepted
Search in section only
You would just pass the other value (i.e. the section handle) in to that template as GET or POST parameter and in the search results template grab the paramater and use it, as in your code.
If using ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
include × 47templating × 23
variable × 9
craft3 × 6
plugin-development × 3
matrix × 3
blocks × 3
php × 2
error-message × 2
macro × 2
search × 1
entry × 1
url × 1
cache × 1
image-transforms × 1
sections × 1
ajax × 1
conditional × 1
forloop × 1
craft2 × 1
javascript × 1
mysql × 1
performance × 1
loop × 1
plugin-supertable × 1