I’m trying to rewrite custom variables to real ee variables. Like {lang:field_name} to {field_name_nl} This is working perfectly with single variables but it won’t work with variable pairs, the variable inside the pair is changed but not rendered. {categories}{lang:category_name}{/categories} is rendered as {categories}{category_name_nl}{/categories} I believe I have to handle the variable pairs in a different way I do with the singles but I can’t get it to work.
this is the code I use to change the single variables, I use the ‘weblog_entries_tagdata’ extension hook
$lang = (((isset($_COOKIE['lang']))?$_COOKIE['lang']:$IN->global_vars['lang']) != $IN->global_vars['lang'])? "_".$_COOKIE['lang']:"";
//loop trough tags
preg_match_all('/'.LD.'(.*?)'.RD.'/', $tagdata, $tags);
//
foreach($tags[1] as $tag)
{
//prepare field_name
$field_name = preg_replace('/(lang:)(.*)/', '$2'.$lang, $tag);
if(!isset($TMPL->var_single[$field_name]))
{
$TMPL->var_single[$field_name] = $field_name;
}
//create and return data
$tagdata = preg_replace('/'.LD.'(lang:)(.*?)'.RD.'/', LD.'$2'.$lang.RD, $tagdata);
$TMPL->log_item("tagdata = ".$tag);
}
return $tagdata;
Can anyone please point me in the right direction? Thanks
Hmm i’m not getting much response on this one. I also think that this question is better moved to the “Technical Assistance” forum.
I’m going to ask my question in a different way.
What I need to do is the moment before the template starts parsing I need to rewrite the template, but it looks like there is no extension hook for that, how can I, without hacking the source files, do something with my templates before the template parser starts?
What I need to do is the moment before the template starts parsing I need to rewrite the template, but it looks like there is no extension hook for that, how can I, without hacking the source files, do something with my templates before the template parser starts?
You are correct that there is no extension hook for this. It’s a bit “too close to the gears” so to speak for us to open it up to the ease of modification that an extension hook would allow.
Have you looked at the wiki articles and forum threads dealing with multi-language sites? There’s no need to rewrite variables on the fly, rather, you use early-parsed variables to “build” the variables dynamically.
Have you looked at the wiki articles and forum threads dealing with multi-language sites? There’s no need to rewrite variables on the fly, rather, you use early-parsed variables to “build” the variables dynamically.
Yes i did, and they where very useful, I tried some of the solutions, but they where not good in this situation.
I just found an easy solution, I only added 2 lines to core.template.php, 2 global variables in path.php and my problem was solved.
If anyone is interested, use at you own risk:
core.template.php (Edit: added a check for the lang cookie) at line 276 add
/** -------------------------------------
/** Custom Hack
/** replace template variables
/** ------------------------------------*/
$suffix = (isset($_COOKIE['lang']))?($_COOKIE['lang'] != $IN->global_vars['default_lang'])?'_'.$_COOKIE['lang']:'':'';
$this->template = preg_replace('/'.LD.'('.$IN->global_vars['variable_prefix'].':)(.*)'.RD.'/', LD.'\2'.$suffix.RD, $this->template);
2 global vars
$global_vars = array(
"default_lang" => "en",
"variable_prefix" => "ulang"
); // This array must be associative
the template
{exp:weblog:entries weblog="{my_weblog}"}
{ulang:title}
{ulang:body}
{categories}
{ulang:category_name}
{/categories}
{/exp:weblog:entries}
Some javascript to set the cookie
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
[removed]= c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
location.reload(true);
}
<a href="#" onclick="setCookie('lang','en',365);" title="English">EN</a>
<a href="#" onclick="setCookie('lang','nl',365);" title="Nederlands">NL</a>
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.