Jump to content

Do Custom Blocks Inherit Variables When Placed in Templates?


GrooveOnBeat

Recommended Posts

As a quick experiment, I dumped the entire globalTemplate content into a custom Page HTML block. I removed all contents from the globalTemplate and inserted the block key. I got this error when viewing the page (as expected):

Quote

[[Template core/front/global/globalTemplate is throwing an error. This theme may be out of date. Run the support tool in the AdminCP to restore the default theme.]]

What I'd like to do is simplify editing multiple templates. For example, if I have 10 templates and I want to make a change that affects all templates I have to manually edit each of the 10 templates to get the same results. If I can use Page blocks like include files, that will make editing much simpler. Then all I have to do is edit 1 Page block instead of editing 10 templates.

Or is there another, and more practical way of achieving this?

Link to comment
Share on other sites

They don't inherit variables - they have their own variable scope.

What you are trying to do would probably be better accomplished by adding custom template bits to your theme. With custom templates, variables aren't inherited either, but you can pass variables into templates like so:

{template="your_template" group="global" app="core" params="$var1, $var2, $etc"}

 

Link to comment
Share on other sites

29 minutes ago, Rikki said:

They don't inherit variables - they have their own variable scope.

What you are trying to do would probably be better accomplished by adding custom template bits to your theme. With custom templates, variables aren't inherited either, but you can pass variables into templates like so:


{template="your_template" group="global" app="core" params="$var1, $var2, $etc"}

 

Thanks for replying, Rikki.  :smile:

By custom template, do I create a new "HTML template"?

MmTq6mn.jpg

In the "variables" field, can I add the variables I want to pass in there?

1l24WIU.jpg

 

Or is it better to add the variables in the "params" when I'm calling the template bit?

If I want to use the template globally, do I choose "System" in Applications?

 

 

Link to comment
Share on other sites

You'd need to add the params to both areas - when you create the template you specify the ones you'll be passing, and then when you actually call the template, pass the through using the params attribute.

It doesn't really matter where you put your custom templates - you can call them wherever they are. I like to create a new group called custom within the System app to keep custom template bits. Then you'd call it like this:

{template="your_template_key" app="core" group="custom" params="$whatever, $you, $specified"}

(In case it isn't clear, the System app key is core).

Link to comment
Share on other sites

12 minutes ago, Rikki said:

You'd need to add the params to both areas - when you create the template you specify the ones you'll be passing, and then when you actually call the template, pass the through using the params attribute.

It doesn't really matter where you put your custom templates - you can call them wherever they are. I like to create a new group called custom within the System app to keep custom template bits. Then you'd call it like this:


{template="your_template_key" app="core" group="custom" params="$whatever, $you, $specified"}

(In case it isn't clear, the System app key is core).

YOU ROCK, RIKKI! :thumbsup: Thanks a bunch!

I don't want to jinx anything but I think it's working.

I created the template and as a test I copied the entire contents of globalTemplate and added them into my custom template. Then I removed everything in globalTemplate, and added my template key. It's working so far, no errors! I say I don't want to jinx anything because it's a feeling like it's too good to be true. :lol:

Here's my template below. I added "location" to it since I left it in the existing global location.

{template="testGlobalTemplate" app="core" location="global" group="custom" params="$title,$html,$location=array()"}

With this adds many other things I've wanted to do. Thank you!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...