/**
  * Build the PHPTAL engine.
  *
  * @param array $options An array of parameters used to set up the PHPTAL configuration.
  *                       Available configuration values include:
  *                       - phptal
  *                       - cache-dir
  *                       - template-dir
  */
 public function __construct(array $options = array())
 {
     if (array_key_exists('phptal', $options)) {
         $this->phptal = $options['phptal'];
         return;
     }
     $this->phptal = new \PHPTAL();
     if (array_key_exists('cache-dir', $options)) {
         $this->phptal->setPhpCodeDestination($options['cache-dir']);
     }
     if (array_key_exists('template-dir', $options)) {
         $this->phptal->setTemplateRepository($options['template-dir']);
     }
 }
 /**
  * @param ContainerInterface $container
  * @return PhptalEngine
  */
 public function __invoke(ContainerInterface $container)
 {
     $config = $container->has('config') ? $container->get('config') : [];
     if (!is_array($config) && !$config instanceof ArrayObject) {
         throw new Exception\InvalidConfigException(sprintf('"config" service must be an array or ArrayObject for the %s to be able to consume it; received %s', __CLASS__, is_object($config) ? get_class($config) : gettype($config)));
     }
     // Set debug mode
     $debug = array_key_exists('debug', $config) ? (bool) $config['debug'] : false;
     $config = $this->mergeConfig($config);
     // Create the engine instance:
     $engine = new PhptalEngine();
     // Change the compiled code destination if set in the config
     if (isset($config['cache_dir'])) {
         $engine->setPhpCodeDestination($config['cache_dir']);
     }
     // Configure the encoding
     if (isset($config['encoding'])) {
         $engine->setEncoding($config['encoding']);
     }
     // Configure the output mode
     $outputMode = isset($config['output_mode']) ? $config['output_mode'] : PHPTAL::HTML5;
     $engine->setOutputMode($outputMode);
     // Set template repositories
     if (isset($config['paths'])) {
         $engine->setTemplateRepository($config['paths']);
     }
     // Configure cache lifetime
     if (isset($config['cache_lifetime'])) {
         $engine->setCacheLifetime($config['cache_lifetime']);
     }
     // If purging of the tal template cache is enabled
     // find all template cache files and delete them
     $cachePurgeMode = isset($config['cache_purge_mode']) ? (bool) $config['cache_purge_mode'] : false;
     if ($cachePurgeMode) {
         $cacheFolder = $engine->getPhpCodeDestination();
         if (is_dir($cacheFolder)) {
             foreach (new DirectoryIterator($cacheFolder) as $cacheItem) {
                 if (strncmp($cacheItem->getFilename(), 'tpl_', 4) != 0 || $cacheItem->isdir()) {
                     continue;
                 }
                 @unlink($cacheItem->getPathname());
             }
         }
     }
     // Configure the whitespace compression mode
     $compressWhitespace = isset($config['compress_whitespace']) ? (bool) $config['compress_whitespace'] : false;
     if ($compressWhitespace) {
         $engine->addPreFilter(new PHPTAL_PreFilter_Compress());
     }
     // Strip html comments and compress un-needed whitespace
     $stripComments = isset($config['strip_comments']) ? (bool) $config['strip_comments'] : false;
     if ($stripComments) {
         $engine->addPreFilter(new PHPTAL_PreFilter_StripComments());
     }
     if ($debug) {
         $engine->setForceReparse(true);
     }
     $this->injectHelpers($engine, $container);
     return $engine;
 }
示例#3
0
 /**
  * Prep the PHPTAL object
  *
  * @param
  *            $app
  */
 public function __construct($app)
 {
     $this->app = $app;
     $this->config = $app['config'];
     $this->phptal = new \PHPTAL();
     // Override the defaults with information from config file
     $preFilters = $this->config->get('phptal.preFilters', []);
     $postFilters = $this->config->get('phptal.postFilters', []);
     $encoding = $this->config->get('phptal.translationEncoding', 'UTF-8');
     $outputMode = $this->config->get('phptal.outputMode', \PHPTAL::HTML5);
     $phpCodeDestination = $this->config->get('phptal.phpCodeDestination', $app['path.storage'] . '/framework/views');
     $forceReparse = $this->config->get('phptal.forceParse', true);
     $templateRepositories = $this->config->get('phptal.templateRepositories', $app['path.base'] . '/resources/views' . (TEMPLATE_ID ? '/' . TEMPLATE_ID : ''));
     $translationClass = $this->config->get('phptal.translationClass');
     $translationDomain = $this->config->get('phptal.translationDomain', 'messages');
     $translationLanguage = [$this->app->getLocale()];
     // Setting up translation settings
     $this->translationSettings['encoding'] = $encoding;
     if (!empty($translationClass)) {
         $this->setTranslator($translationLanguage, $translationDomain, $translationClass);
     }
     // Setting up all the filters
     if (!empty($preFilters)) {
         foreach ($preFilters as $filter) {
             $this->phptal->addPreFilter($filter);
         }
     }
     if (!empty($postFilters)) {
         $filterChain = new PHPTALFilterChain();
         foreach ($postFilters as $filter) {
             $filterChain->add($filter);
         }
         $this->phptal->setPostFilter($filterChain);
     }
     $this->phptal->setForceReparse($forceReparse);
     $this->phptal->setOutputMode($outputMode);
     $this->phptal->setTemplateRepository($templateRepositories);
     $this->phptal->setPhpCodeDestination($phpCodeDestination);
 }
 /**
  * Create an instance of PHPTAL and initialize it correctly.
  *
  * @return     PHPTAL The PHPTAL instance.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      1.0.2
  */
 protected function createEngineInstance()
 {
     $phptalPhpCodeDestination = AgaviConfig::get('core.cache_dir') . DIRECTORY_SEPARATOR . AgaviPhptalRenderer::COMPILE_DIR . DIRECTORY_SEPARATOR . AgaviPhptalRenderer::COMPILE_SUBDIR . DIRECTORY_SEPARATOR;
     // we keep this for < 1.2
     if (!defined('PHPTAL_PHP_CODE_DESTINATION')) {
         define('PHPTAL_PHP_CODE_DESTINATION', $phptalPhpCodeDestination);
     }
     AgaviToolkit::mkdir($phptalPhpCodeDestination, fileperms(AgaviConfig::get('core.cache_dir')), true);
     if (!class_exists('PHPTAL')) {
         require 'PHPTAL.php';
     }
     $phptal = new PHPTAL();
     if (version_compare(PHPTAL_VERSION, '1.2', 'ge')) {
         $phptal->setPhpCodeDestination($phptalPhpCodeDestination);
     }
     return $phptal;
 }
示例#5
0
 /**
  *
  * @return PHPTAL
  */
 protected function getTAL()
 {
     try {
         if (!$this->TAL instanceof PHPTAL) {
             $this->TAL = new PHPTAL($this->getPathTemplate());
         }
         $translator = new LBoxTranslator($this->getPathTemplate());
         // zajistit existenci ciloveho adresare PHP kodu pro TAL:
         $phptalPhpCodeDestination = LBoxUtil::fixPathSlashes(LBoxConfigSystem::getInstance()->getParamByPath("output/tal/PHPTAL_PHP_CODE_DESTINATION"));
         LBoxUtil::createDirByPath($phptalPhpCodeDestination);
         $this->TAL->setTranslator($translator);
         $this->TAL->setForceReparse(LBoxConfigSystem::getInstance()->getParamByPath("output/tal/PHPTAL_FORCE_REPARSE"));
         $this->TAL->setPhpCodeDestination($phptalPhpCodeDestination);
         $this->TAL->SELF = $this;
         return $this->TAL;
     } catch (Exception $e) {
         throw $e;
     }
 }
 /**
  * Create an instance of PHPTAL and initialize it correctly.
  *
  * @return     PHPTAL The PHPTAL instance.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      1.0.2
  */
 protected function createEngineInstance()
 {
     $phptalPhpCodeDestination = AgaviConfig::get('core.cache_dir') . DIRECTORY_SEPARATOR . AgaviPhptalRenderer::COMPILE_DIR . DIRECTORY_SEPARATOR . AgaviPhptalRenderer::COMPILE_SUBDIR . DIRECTORY_SEPARATOR;
     // we keep this for < 1.2
     if (!defined('PHPTAL_PHP_CODE_DESTINATION')) {
         define('PHPTAL_PHP_CODE_DESTINATION', $phptalPhpCodeDestination);
     }
     AgaviToolkit::mkdir($phptalPhpCodeDestination, fileperms(AgaviConfig::get('core.cache_dir')), true);
     if (!class_exists('PHPTAL')) {
         require 'PHPTAL.php';
     }
     $phptal = new PHPTAL();
     if (version_compare(PHPTAL_VERSION, '1.2', 'ge')) {
         $phptal->setPhpCodeDestination($phptalPhpCodeDestination);
     } else {
         trigger_error('Support for PHPTAL versions older than 1.2 is deprecated and will be removed in Agavi 1.2.', E_USER_DEPRECATED);
     }
     if ($this->hasParameter('encoding')) {
         $phptal->setEncoding($this->getParameter('encoding'));
     }
     return $phptal;
 }
示例#7
0
 private function display_tal(midgardmvc_core_request $request, $content, array $data)
 {
     $tal = new PHPTAL($request->get_template_identifier());
     $tal->setPhpCodeDestination($this->midgardmvc->cache->template->get_cache_directory());
     $tal->uimessages = false;
     if ($this->midgardmvc->configuration->enable_uimessages) {
         if ($this->midgardmvc->uimessages->has_messages() && $this->midgardmvc->uimessages->can_view()) {
             $tal->uimessages = $this->midgardmvc->uimessages->render();
         }
     }
     $tal->midgardmvc = $this->midgardmvc;
     $tal->request = $request;
     // FIXME: Remove this once Qaiku has upgraded
     $tal->MIDCOM = $this->midgardmvc;
     foreach ($data as $key => $value) {
         $tal->{$key} = $value;
     }
     $tal->setSource($content);
     $translator =& $this->midgardmvc->i18n->set_translation_domain($request->get_component()->name);
     $tal->setTranslator($translator);
     try {
         $content = $tal->execute();
     } catch (PHPTAL_TemplateException $e) {
         throw new midgardmvc_exception("PHPTAL: {$e->srcFile} line {$e->srcLine}: " . $e->getMessage());
     }
     return $content;
 }
示例#8
0
    /**
     * Renders a template.
     * @param string $name       A template name
     * @param array  $parameters An array of parameters to pass to the template
     *                           the default options engine can be override by $parameters['_engine_'][option]
     * @return string The evaluated template as a string
     *
     * @throws \InvalidArgumentException if the template does not exist
     * @throws \RuntimeException         if the template cannot be rendered
     */
    public function render($name, array $parameters = array() )
    {

        $template = new \PHPTAL();


        // engine options by parameters to relpace configuration
        if(isset($parameters['_engine_'])&&(is_array($parameters['_engine_']))){
            $options = $parameters['_engine_'];
        }else{
            $options = array();
        }

        // code cache destination
        $tmpdir = (isset($options['cache_dir']))?$options['cache_dir']:$this->options['cache_dir'];
        if(!is_dir($tmpdir)){mkdir($tmpdir);}
        $template->setPhpCodeDestination($tmpdir);

        // code cache durration
        $template->setCacheLifetime( (isset($options['cache_dir']))?$options['cache_lifetime']:$this->options['cache_lifetime'] );

        // encoding
        $template->setEncoding( (isset($options['charset']))?$options['charset']:$this->options['charset'] );
  
        // output mod
        if(!isset($options['output_format'])){
            $options['output_format'] = $this->options['output_mode'];
        }
        if($options['output_format']=='XHTML'){
            $template->setOutputMode( \PHPTAL::XHTML );
        }elseif($options['output_format']=='HTML5'){
            $template->setOutputMode( \PHPTAL::HTML5 );
        }elseif($options['output_format']=='XML'){
            $template->setOutputMode( \PHPTAL::XML );
        }else{
            throw new \InvalidArgumentException('Unsupported output mode ' . $options['output_format']);
        }

        // force reparse (for debug prefilter)
        $template->setForceReparse( (isset($options['force_reparse']))?$options['force_reparse']:$this->options['force_reparse'] );


        // pre filters
        $filtres = $this->options['pre_filters'];
        foreach($filtres as $filtre){
            $template->addPreFilter( new $filtre['class']($filtre['params']) );
        }

        // post filters
        $filtres = $this->options['post_filters'];
        if($filtres){
            $template->setPostFilter(new PhptalPostFilters($filtres));
        }

        // set SourceResolver
        if(!isset($options['resolver'])){
            $template->addSourceResolver($this->resolver);
        }else{
            if($this->container->has($options['resolver'])){
                $resolver = $this->container->get($options['resolver']);
                $r = new \ReflectionClass( get_class($resolver) );
                if (!$r->implementsInterface('Neni\\PhptalBundle\\Phptal\\PhptalResolverInterface')) {
                    throw new \InvalidArgumentException(sprintf('The service "%s" does implements PhptalResolverInterface.', $options['resolver']));
                }else{
                    $template->addSourceResolver( $resolver );
                }
            }else{
                throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $options['resolver']));
            }
        }

        // set source template
        $template->setTemplate($name);


        // set data
        if(!isset($options['populater'])){
            unset($parameters['_engine_']);
            $this->populater->populate($template, $parameters);
        }else{
            if($this->container->has($options['populater'])){
                $populater = $this->container->get($options['populater']);
                $r = new \ReflectionClass( get_class($populater) );
                if (!$r->implementsInterface('Neni\\PhptalBundle\\Phptal\\PhptalPopulaterInterface')) {
                    throw new \InvalidArgumentException(sprintf('The service "%s" does implements PhptalPopulaterInterface.', $options['populater']));
                }else{
                    unset($parameters['_engine_']);
                    $populater->populate($template, $parameters);
                }
            }else{
                throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $options['populater']));
            }
        }


        // generic helper
        $template->Helper = new PhptalGenericHelper($this->container, $parameters);

        // perform
        try{
            $result = $template->execute();
        }catch (PHPTAL_TemplateException $e){
            throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
        }

        return $result;
    }
示例#9
0
文件: View.php 项目: jo-m/ecamp3
 /**
  * Set the directory used to save generated templates.
  *
  * @param string $path The path to use.
  *
  * @return void
  */
 public function setCacheDirectory($path)
 {
     $this->_engine->setPhpCodeDestination($path);
 }