Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ExpressionEngine Core 1.6.8 - PHP or EE Template-Level Variables...

Featured Replies

Hi guys,

 

I have come across an issue which I can't seem to work around in EE Core 1.6.8, using either PHP or EE template-level variables.

 

I have a main template, with a nested template (a simplified version is shown below).

 

Main Template

{!-- Main Template --}

<?php
 $martins_variable = "THIS IS AN INTERESTING VARIABLE";
?>

<html>

 <head>
   ...SNIP...
 </head>

 <body>

   {embed="site/nested_template"}

 </body>

</html>

 

 

Embedded Template site/nested_template

{!-- Nested Template --}

The variable is <?php echo $martins_variable; ?>

 

 

PHP is enabled on every template (for now), and I have tried this parsing PHP at both input and output.

 

It equally doesn't work with EE template-level variables using {assign_variable:martins_variable="THIS IS AN INTERESTING VARIABLE" in the main template and then calling it in the nested template with {martins_variable}.

 

Is there a way around this? I would really like to be able to do this using one or the other method (it doesn't really matter which, but PHP would be preferable). I don't want to assign global variables in EE because I want to use these variables to assign things like background colours and CSS styles to different templates, so I need the variables in the "top level" templates.

 

Many thanks,

 

Martin

  • Author

Sorted (using EE variables). I needed to pass the variable in the embed tag, and then use "embed:" before the variable name in the nested template.

 

Slightly long winded, but I can see that it gives you very granular control over variables in templates.

 

Main Template

{!-- Main Template --}

{assign_variable:martins_variable="THIS IS AN INTERESTING VARIABLE"}

<html>

 <body>

   {embed="site/nested_template" martins_variable="{martins_variable}"}

 </body>

</html>

 

 

Embedded Template site/nested_template

{!-- Nested Template --}

The variable is {embed:martins_variable}

 

Cheers!

 

Martin

I see you got it sorted!

 

You can also store bits of data in Templates>Global variables and call them in anywhere they're needed.

  • Author

Thanks! I didn't want to use global variables because I wanted to use variables to trigger certain behaviours depending on the template. At the top of each template file I have:

 

{!-- Website root URL --}
{assign_variable:website_root="http://localhost/site/"}

{!-- Internal reference ID of page --}
{assign_variable:page_id="home"}

{!-- Page title --}
{assign_variable:page_title="Home"}

{!-- Page heading (text used for <h1> tag) --}
{assign_variable:page_heading="This is a Heading"}

{!-- Page colour (used for background and links etc.) --}
{assign_variable:page_color="#0d507a"}

{!-- Page META description --}
{assign_variable:meta_description="META Description Goes Here"}

{!-- Page META keywords --}
{assign_variable:meta_keywords="META Keywords Go Here"}

{!-- Background image position --}
{assign_variable:bg_position_left="560"}
{assign_variable:bg_position_top="0"}

{!-- Heading 1 image margins --}
{assign_variable:h1_margin_top="30"}
{assign_variable:h1_margin_right="0"}
{assign_variable:h1_margin_bottom="40"}
{assign_variable:h1_margin_left="0"}

 

I can't put these in a global variables (with the possible exception of website_root - see other post for that!!) because they are different 'per template'.

 

Cheers!

 

Martin

  • Author

Why not use weblog entries and/or weblog categories to generate the dynamic data?

 

I did think about that, but I thought it might overcomplicate things - each template is a different 'design' anyway, so separate template files have to exist. Keeping the variables in the header of the template file keeps everything is in one place. There are only 8 different templates anyway.

 

I'm using weblogs for news, downloads, and resources (a links directory).

If it work for you then that's fair enough - there are always many ways to do things in EE, the freedom to choose how you do things is the best bit :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.