Invenzzia »

Pages: [1]   Go Down
  Print  
Author Topic: OPT: include doesn't work  (Read 3342 times)
0 Members and 1 Guest are viewing this topic.
wimg
User

Offline Offline

Posts: 6


View Profile
« on: February 25, 2008, 18:41:55 »

I created a template called header.tpl which contains nothing more than a <HTML> tag.

I now want to include this in my other templates, so I tried :
{include="header.tpl"}
This results in an empty page

Also tried :
{include=`header.tpl`}
Same result

Also tried :
{include file=`header.tpl`}
which gives :
Required parameter "file" not specified in "include" instruction.

And also tried :
{include file="`header.tpl`"}
which also gives that error.

Any idea what's wrong here ?
Logged
eXtreme
Invenzzia
Administrator
User
*****
Offline Offline

Posts: 129

Jacek Jędrzejewski


View Profile WWW
« Reply #1 on: February 26, 2008, 14:20:44 »

Have you got empty result without this {include} instruction?
Which output function do you use: fetch or parse? Do you send the output? :P

Check also paths. For example:
Templates path is /templates/ so you have there:
/templates/site/
/templates/site/foo.tpl
/templates/site/header.tpl

in foo.tpl you have to include header.tpl using "site/header.tpl"

No other ideas.
Logged

Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #2 on: February 26, 2008, 19:58:31 »

I would say the paths are incorrect. By default, {include} does not generate any exception in case the specified template does not exist. This is done, because you can specify a default template to execute then, for example:

Code:
{include=$someTemplate; `default.tpl`}

Some facts concerning paths:
 - The path MUST end with "/", for example ./templates/
 - If you have a template in a subdirectory, you have to specify that directory together with the template name, as eXtreme said.
Logged

PozDrX, Zyx
---Invenzzia group---
wimg
User

Offline Offline

Posts: 6


View Profile
« Reply #3 on: March 21, 2008, 13:19:27 »

It seems using paths such as "../../header.tpl" doesn't work. It results in : Unexpected error : Unexpected token: OPCODE_UNKNOWN (.) in expression ../../../../layouts/header.tpl
Logged
wimg
User

Offline Offline

Posts: 6


View Profile
« Reply #4 on: March 21, 2008, 13:21:44 »

Also, I get this in the notices :
filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for ../tmp/view_compiles/%%index__index.tpl.php

What's the actual working directory for OPT ?

I'm trying to implement it in Zend Framework 1.5.
Logged
Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #5 on: March 21, 2008, 14:02:51 »

To the first post -> OPT is quite more demanding, when it comes to string support than Smarty. As far as I know, Smarty allowed something like this:

Code:
{include file=../template.tpl}

In OPT: First of all, you must remember about the double quotes around the attribute value: {include file="../template.tpl"} Moreover, this attribute expects an expression. That means, you can read its value from a block etc. But if you want to specify a constant string, you have to specify it according to the syntax rules. And the syntax says that only a single word beginning with a letter or underline, containing only letters, number and underline, which is not an operator can be written without quotes. Take a look at your path: ../template. Is this a single word beginning with a letter? No. So the quotes are obligatory. Finally, you have something like this:

Code:
{include file="`../template.tpl`"}

In OPTv2 the syntax is quite more clever, because you can move the attribute to the "str" namespace:

Code:
<opt:include str:file="../template.tpl" />

To the second post -> can you tell me, what you have done to the library, that it displays notices even if all the calls of filemtime() are preceded by the @ operator? :) But to be more serious... I think the problem lies in mixing relative and absolute paths together with include_path option. This unsually goes straight to a disaster. You control the location of source and compiled templates with the root and compile directives. Check, what is the working directory of the script calling OPT and then try to follow this directory along the ../tmp/view_compiles path. I suppose that it is invalid and points to the wrong or unexisting directory, because the script working directory is in fact different that you've assumed during writing it.

BTW. I made one project using OPT+OPF+Zend Framework, but unfortunately, I don't have its source code with me now. If you are interested, I can provide you the parts with my directory path management, but in the middle of next week.

PS. Use the "Edit" option next time instead of writing two posts :).
Logged

PozDrX, Zyx
---Invenzzia group---
wimg
User

Offline Offline

Posts: 6


View Profile
« Reply #6 on: March 21, 2008, 14:22:43 »

In OPTv2 the syntax is quite more clever, because you can move the attribute to the "str" namespace:
Code:
<opt:include str:file="../template.tpl" />
I don't suppose OPTv2 is close to being released at this point ?

To the second post -> can you tell me, what you have done to the library, that it displays notices even if all the calls of filemtime() are preceded by the @ operator? :) But to be more serious... I think the problem lies in mixing relative and absolute paths together with include_path option. This unsually goes straight to a disaster. You control the location of source and compiled templates with the root and compile directives. Check, what is the working directory of the script calling OPT and then try to follow this directory along the ../tmp/view_compiles path. I suppose that it is invalid and points to the wrong or unexisting directory, because the script working directory is in fact different that you've assumed during writing it.
I only saw those notices when live debugging with Zend Studio in the Zend Studio console. Didn't think it was a problem, but I thought I'd mention it anyway.

I believe I've discovered the correct path now, although it's still pretty tricky to use in ZF.

BTW. I made one project using OPT+OPF+Zend Framework, but unfortunately, I don't have its source code with me now. If you are interested, I can provide you the parts with my directory path management, but in the middle of next week.
That might be very useful !


I have a feature request as well. Might be better to create a separate topic for it, but I thought I'd ask right away : would it be possible to use something else than files for writing to the compile and cache dirs ? For example : we'd like to store this data in memcache.
Furthermore, a feature we miss that we had in Smarty, is the compile_id parameter, which basically allowed the use of a single compile and cache directory, even when using multiple template directories.
Both of the above combined are very powerful, since they allow you to memcache your entire site, which works especially well if you have multiple frontend servers.
Any ideas ?   
« Last Edit: March 21, 2008, 15:25:39 by wimg » Logged
Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #7 on: March 25, 2008, 19:05:43 »

In OPT 1, the caching is strictly integrated with the parser. In order to write your own handler, you must replace the cacheProcess(), cacheWrite(), isCached() and cacheReset() methods. The description:

 - isCached() - it takes the filename and optionally the ID. Its purpose is to check, whether the template is cached. It returns true or false.
 - cacheProcess() - if the template is cached, it reads its and displays. Otherwise it runs the output buffering.
 - cacheWrite() - it takes two parameters: the compiled template filename and the information whether the cache has some dynamic parts or not. It captures the parser output and saves it to the file. Beware of the dynamic parts. You have to read the file $this->compile.$compiled.'.dyn' and unserialize its content. It contains all the PHP code for the dynamic parts used by this template. You have to mingle them with the static content. The best idea is to copy the original code and just replace the parts that save everything to disk :).

In OPT 2, the output caching is already done as a separate class, so there's no problem to replace it. But the compiled templates are still hardcoded to be stored on the HDD.

When it comes to the "compile_id" -> OK, I'll add it. However, in OPT 1, you should have no problem with mixing the compiled and cached templates in one directory, because the library generates quite different names for each type.
Logged

PozDrX, Zyx
---Invenzzia group---
wimg
User

Offline Offline

Posts: 6


View Profile
« Reply #8 on: March 26, 2008, 12:02:13 »

Is OPT2 close to release date ?

The main issue is that we have multiple template directories, but we prefer to have 1 compile and 1 cache directory. since we have multiple index.tpl files, they are compiled under the same name, so they don't overwrite eachother if one file is newer than the other. This is the reason we want to specify a compile_id, so we can differentiate between the different index.tpl files by using an md5 of the directory as compile_id.

The memcache implementation is not a priority for us at this point. We use memcache for a range of other things, so we thought it could have been useful, but template caching on disk is fast enough at this point.
Logged
Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #9 on: March 26, 2008, 14:12:43 »

Ad. OPT2 -> at this point, I assume that about 80% of the library is completed and the rest will be added soon. But this is only a development version and it was not tested on the real website yet. Once the code is completed, there will be a beta release, then at least one Release Candidate and at last, the final version. I work as quick as possible, because we need this library, too :).

Despite the library, I write the documentation at the same time, but it is a bit delayed. Currently, I write only the Polish version and it will be translated into English later (but still before the final release).

But going back to the topic... I've done some research and added compileId to my local code. Moreover, I found a small bug in a rarely used feature :). In the weekend, I'll release OPT 1.1.4 with these updates, because I have to do some necessary tests and update the manual.

BTW. I have these files from Zend Framework + OPT connection. My code is not complicated:

bootstrap.php

Code:
<?php
define
('OPT_DIR'BOOT_LOCATION.'library/ext/opt/');
define('OPF_DIR'BOOT_LOCATION.'library/ext/opf/');

define('DIR_TPLCC'BOOT_LOCATION.BOOT_APPNAME.'/data/tpl_compile/');
define('DIR_TPL_CACHE'BOOT_LOCATION.BOOT_APPNAME.'/data/tpl_cache/');

// I include the library manually.
require(OPT_DIR.'opt.class.php');
require(
OPF_DIR.'opf.class.php');

...

// OPT
$view = new My_View;
$view -> setScriptPath(NULL);
$view -> setI18n($translate);
Zend_Registry::set('view'$view);

...

?>

My/View.php

Code:
<?php

class 
My_View implements Zend_View_Interface
{
const ERROR 0;
const INFO 1;

private $_tpl;
private $templates;
private $renderer NULL;
private $overall true;

private $title;
private $navigation = array();

public function __construct()
{
$config Zend_Registry::get('config');

$this -> _tpl = new optClass;
$this -> _tpl -> compile DIR_TPLCC;
$this -> _tpl -> cache DIR_TPL_CACHE;

$this -> _tpl -> xmlsyntaxMode true;
$this -> _tpl -> gzipCompression false;

$this -> templates = array();
// end __construct();

public function getEngine()
{
return $this -> _tpl;
// end getEngine();

public function setScriptPath($name)
{
if(is_null($name))
{
$this -> _tpl -> root BOOT_LOCATION.BOOT_APPNAME.'/views/';
$this -> _tpl -> compile DIR_TPLCC;
}
else
{
$this -> _tpl -> root BOOT_LOCATION.BOOT_APPNAME.'/views/'.$name.'/';
$this -> _tpl -> compile DIR_TPLCC.$name.'/';
}
// end setScriptPath();

public function __set($key$value)
{
$this -> _tpl -> assign($key$value);
// end __set();

public function __get($key)
{
return $this -> _tpl -> data[$key];
// end __get();

public function __isset($key)
{
return isset($this -> _tpl -> data[$key]);
// end __isset();

public function __unset($key)
{
unset($this -> _tpl -> data[$key]);
// end __unset();

public function assign($name$value NULL)
{
$this -> _tpl -> assign($name$value);
// end assign();

public function clearVars()
{
$this -> _tpl -> data = array();
// end clearVars();

public function render($name)
{
$this -> templates = array(=> $name);
$session Zend_Registry::get('session');
$session -> save();
$this -> display();
die();
// end render();

public function setI18n(ioptI18n $i18n)
{
$this -> _tpl -> setObjectI18n($i18n);
// end setI18n();

public function addNavigation($name$address)
{
$this -> navigation[] = array('name' => $name'address' => $address);
// end addNavigation();

public function addPages($pages)
{
$translate Zend_Registry::get('translate');
$this -> _tpl -> assign('which'$translate -> putApply('global''pageStats'$pages->getActive(), $pages->getSize()));
$this -> _tpl -> assign('pages'$pages);
// end addPages();

public function systemPrepare()
{
$data Zend_Registry::get('data');
if(!is_null($this -> renderer))
{
$renderer My::loadRenderer($this -> renderer);
}
else
{
$renderer My::loadRenderer($data->domain['hash']);
}
$renderer -> systemPrepare($this);
$this -> renderer $renderer;
// end systemPrepare();

public function display()
{
$config Zend_Registry::get('config');
$data Zend_Registry::get('data');

$this -> _tpl -> charset 'utf-8';
if($config -> terenzzia -> debug)
{
$this -> _tpl -> httpHeaders(OPT_HTML);
}
else
{
$this -> _tpl -> httpHeaders(OPT_HTML);
}

$this -> renderer -> prepare($this);

$this -> _tpl -> assign('address'$config -> website -> address);
$this -> _tpl -> assign('media'$config -> website -> media);
$this -> _tpl -> assign('navigation'$this -> navigation);
$this -> _tpl -> assign('title'$this -> title.' - '.$data->domain['title']);

$session Zend_Registry::get('session');
$session -> save();

$this -> _tpl -> setMasterTemplate('master.tpl');
if($this -> overall)
{
$this -> _tpl -> parse('overall_header.tpl');
}
foreach($this -> templates as $tpl)
{

$this -> _tpl -> parse($tpl);
}

if($this -> overall)
{
$this -> _tpl -> parse('overall_footer.tpl');
}
// end display();

public function setTemplate($tpl)
{
$this -> templates[] = $tpl;
// end setTemplate();

public function setTitle($title)
{
$this -> title $title;
// end setTitle();

public function setOverall($overall)
{
$this -> overall $overall;
// end setOverall();

public function setRenderer($renderer)
{
$this -> renderer $renderer;
// end setRenderer();

public function message($type$message$redirect '')
{
if($type == Terenzzia_View::ERROR)
{
$this -> error true;
}
else
{
$this -> error false;
}
$this -> message $message;
$this -> redirect $redirect;
$this -> render('message.tpl');
// end message();

public function question($message$redirect)
{
$opf Zend_Registry::get('opf');
$val Zend_Registry::get('validator');

if($val -> map('ans'OPF_GET, new opfStandardContainer(
new opfConstraint(MAP_TYPETYPE_STRING),
new opfConstraint(MAP_MATCHTO'/yes|no/')
)))
{
if($val -> ans == 'yes')
{
return true;
}
return false;
}

$router Zend_Registry::get('router');

$this -> message $message;

$redirect['ans'] = 'yes';
$this -> urlYes $router -> createURL($redirect);

$redirect['ans'] = 'no';
$this -> urlNo $router -> createURL($redirect);

$this -> render('question.tpl');
// end question();
// end My_View;
?>

As you see, all the path-related issues are done in one method: setScriptPath(). Writing your own handler for OPT is not necessary (in fact, the basic thing you have to do is to load the library and create an object), but it helps in some issues. All the paths are defined in bootstrap.php file as constants, so that it's easy to manage them.
Logged

PozDrX, Zyx
---Invenzzia group---
Pages: [1]   Go Up
  Print  
 
Jump to:  

Subject Started by Replies Views Last post
abercrombie and fitch, was actually full of flaws galardef 0 35 Last post May 05, 2012, 09:18:50
by galardef
OPTv2: OPT 2.1 - ideas and proposals Zyx 0 959 Last post March 14, 2009, 22:57:23
by Zyx
abercrombie, tail hair or waste products. In 1997 galardef 0 26 Last post May 05, 2012, 09:16:57
by galardef
abercrombie, her family galardef 0 61 Last post April 05, 2012, 03:17:23
by galardef
abercrombie and fitch, a harmonious socialist society galardef 0 70 Last post April 16, 2012, 04:28:06
by galardef