2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FuelPHPでMustacheテンプレートのpartials機能を利用する方法

Posted at

以下のようにparserパッケージ修正したのち、コンフィグファイルの設定を行うと利用できる。
なぜにデフォルトで使えない。。。

parser/classes/view/mustache.php
namespace Parser;

use Mustache_Engine;
// 追記
use Mustache_Loader_FilesystemLoader;

class View_Mustache extends \View
{
	protected static $_parser;

	protected function process_file($file_override = false)
	{
		$file = $file_override ?: $this->file_name;
		$data = $this->get_data();

		try
		{
			return static::parser()->render(file_get_contents($file)
, $data);
		}
		catch (\Exception $e)
		{
			// Delete the output buffer & re-throw the exception
			ob_end_clean();
			throw $e;
		}
	}

	public $extension = 'mustache';

	/**
	 * Returns the Parser lib object
	 *
	 * @return  Mustache_Engine
	 */
	public static function parser()
	{
		if ( ! empty(static::$_parser))
		{
			return static::$_parser;
		}

		$options = array(
			// TODO: set 'logger' with Monolog instance.
			'cache'   => \Config::get('parser.View_Mustache.environm
ent.cache_dir', APPPATH.'cache'.DS.'mustache'.DS),
			'charset' => \Config::get('parser.View_Mustache.environm
ent.charset', 'UTF-8'),
		);

		if ($partials = \Config::get('parser.View_Mustache.environment.p
artials', array())) {
			$options['partials'] = $partials;
		}

		if ($helpers = \Config::get('parser.View_Mustache.environment.he
lpers', array())) {
			$options['helpers'] = $helpers;
		}

       // 追記(ディレクトリは好きな位置を指定)
     $options['partials_loader'] =
         new Mustache_Loader_FilesystemLoader(
           dirname(__FILE__) . '/../../../../app/views');
    
		static::$_parser = new Mustache_Engine($options);

		return static::$_parser;
	}
}

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?