Page 1 of 1

difference between "src" and "call" to display values

Posted: Sun Oct 30, 2005 3:41 am
by wene
hello
i would like to re-use templates, so i have played with the "call" function.
but when i use this, i can see that the datas are not displayed in my niew template.

i can see the good values inside the dump(), but they dont appear inside the one i called.

when i use src, the values appear, but i cant repeat the template because already loaded.

how to render my values with "call" function ?

Re: difference between "src" and "call" to display values

Posted: Sun Oct 30, 2005 8:11 am
by alwarren
Try using "unusedvars = ignore" in your called template. Has something to do with the order things are parsed. Not sure I understand it but it works for me.

Code: Select all

Php: $tmpl->addVar('tempname', 'varname', '$value');

Calling template:
<mos:tmpl name="tempname">
	<mos:Call template="temp2name" varname="{VARNAME}">
<mos:/tmpl>

Called template:
<mos:tmpl name="temp2name" unusedvars="ignore">
	<div>{VARNAME}</div>
</mos:tmpl>
Also if you have your called templates in a separate file, you must read them first.

Code: Select all

$tmpl->readTemplatesFromInput( 'called.tmpl' );
$tmpl->readTemplatesFromInput( 'calling.tmpl' );
On a side note. I've disovered that in most cases the "varname={VARNAME}" isn't needed. In some examples I've seen it's used. I wound up removing it from all my calling templates. Might have something to do with varscope. Not sure. Still in the learning curve here.

Re: difference between "src" and "call" to display values

Posted: Sun Oct 30, 2005 4:52 pm
by wene
--edit--
this simplified example didnt give me the same result than my prog, so i modified the php code to get the same result.
it is funny, that it loads the values only when i create a function...
i can also do :
function justreturn ($tmpl){
    return $tmpl;
    }
$tmpl =& justreturn($tmpl);
$tmpl -> addVar ( 'test', 'testvar', 'hello' );
it works the same ... but my prob is not solved
--/edit--


php job :

Code: Select all

<?php
//--- i create the patTemplate object and read the needed template
    require_once( $mosConfig_absolute_path . '/includes/patTemplate/patTemplate.php' );
    $tmpl =& patFactory::createTemplate( '', false, false );
    $tmpl->setRoot( dirname( __FILE__ ) . '/tmpl' );

//--- i read the desired called template.
    $tmpl->readTemplatesFromFile( 'test.html' );

//--- i give a value to the desired var
// to get the same result than my program, i had to build a function.
    function feedVar ($tmpl){
        $tmpl -> addVar ( 'test', 'testvar', 'hello' );
        return $tmpl;
    }
    $tmpl = & feedVar($tmpl);
    
//--- i read and display the "master" template
    $tmpl->readTemplatesFromFile( 'master.html' );
    $tmpl->displayParsedTemplate( 'master' );
?>
master.html :

Code: Select all

<mos:tmpl name="master">
    test=<mos:call template="test"/> 
    test2=<mos:call template="test"/> 
    test3=<mos:call template="test"/>
</mos:tmpl>
test.html :

Code: Select all

<mos:tmpl name="test">
    {TESTVAR}
<mos:tmpl>
when i render this, i can see that the variable has got the good value in the dump() window.
like this:


Template "test"
Variables
Name        Assigned Value      Modifier

TESTVAR    hello                    [none]

i can not see the "hello" in the master template.
in my output, i see :
test= test2= test3=
and i would like to see:
test=hello test2=hello test3=hello

i can see the good value when i use "src" instead of "call" but, in this case, i cant repeat the template.

[solved] Re: difference between "src" and "call" to display values

Posted: Sun Oct 30, 2005 6:30 pm
by wene
It works !!!
thank you Alwarren !!
i have used unusedvars="ignore" on the test template like this :

Code: Select all

<mos:tmpl name="test" unusedvars="ignore">
    {TESTVAR}
<mos:tmpl>
then , i had to assign the value to the master template like this :

Code: Select all

$tmpl -> addVar ( 'master', 'testvar', 'hello' );
and not like :

Code: Select all

$tmpl -> addVar ( 'test', 'testvar', 'hello' );
this is exactly what iam dreaming about for 2 days... :)