We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Parse template/tags and save output

Development and Programming

eeart's avatar
eeart
257 posts
15 years ago
eeart's avatar eeart

I’m trying to make a little plugin that caches templates on its own.

Instead of using the following line, which caches the template per URL:

{embed="group/template" cache="yes" refresh="60"}

I’m trying to make an add-on that parses and caches the template itself:

{exp:myaddon source="group/template" myrefresh="60"}

The idea is that the add-on will get the template from exp_templates, parse it and save it in a text file for 60 minutes. If a cache is available it will return the cache. The advantage is that the template is not cached per URL.

My question is: After I get ‘template_data’ from the database, what function can I use to parse the data?

I tried TMPL->parse_variables but this doesn’t work. The output still has all the EE tags.

Thank you.

       
eeart's avatar
eeart
257 posts
15 years ago
eeart's avatar eeart

Is this not possible?

       
the3mus1can's avatar
the3mus1can
426 posts
15 years ago
the3mus1can's avatar the3mus1can

Look at the following methods in the system/expressionengine/libraries/Template.php file:

run_template_engine fetch_and_parse

       
eeart's avatar
eeart
257 posts
15 years ago
eeart's avatar eeart

Thanks, this has been really helpful.

I am now using the following code to parse a template in my module:

$this->EE->TMPL->fetch_and_parse($template_group, $template, $sub = FALSE, $site_id);
$output = $this->EE->TMPL->parse_globals($this->EE->TMPL->final_template);

Then I replace variables with parse_variables_row. With the following {var} gets replaced with ‘abc’:

$variables = array('var' => 'abc');
$output = $this->EE->TMPL->parse_variables_row($output, $variables);

My two questions:

  1. Is there also a way to parse embedded variables in the template, so ones like: {embed:var} ?

  2. What is the $sub for? If I set it to TRUE the output is empty.

Thanks!

       
the3mus1can's avatar
the3mus1can
426 posts
15 years ago
the3mus1can's avatar the3mus1can
  1. Is there also a way to parse embedded variables in the template, so ones like: {embed:var} ?

Yes, kind of. If you pass $sub = FALSE then it will parse any emebbed templates that are in the master template. The embed vars will be parsed naturally. If you are calling the embed tempate not in the context of the master template then you will have to manually parse them yourself.

  1. What is the $sub for? If I set it to TRUE the output is empty.

You want $sub to be false. It tells the parser if it is operating on the master template or a sub template (embeds). Only the master template output is assigned to $this->EE->TMPL->final_template.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.