0
0

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.

【質問】Twigで変数から変数を呼び出せない

Posted at

Twig使われている方でわかる方いらしたら教えて下さい。

Twigをテンプレートエンジンにして、簡易的なCMSを作ってます。

コンテンツ側から、設定などの変数を呼び出したいのですがうまく行かず。
テンプレートエンジン自体にあまり慣れてないので、何か根本的に間違ってる気がしますが・・

index.php
# コンテンツを変数に格納
$content = file_get_contents("./content/20130101.html");

# 設定
$vars = array(
	"site" => array(
		"name" => "HOGE Blog",
		"img_path" => "./inc/images"
	),
	"content" => $content
);

# Twigを読み込んで表示
require_once "./twig/Autoloader.php";
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem("./");
$twig   = new Twig_Environment($loader);
$twig->display("template.html", $vars);
template.html
<!DOCTYPE html>
<html lang="ja">
<head><title>{{ site.name }}</title></head>
<body>{{ content|raw }}</body>
</html>
./content/20130101.html
<p>This blog name is {{ site.name }}.</p>
<dl>
	<dt>My photo<dt>
	<dd><img src="{{ site.img_path }}/myphoto.jpg"></dd>
</dl>
0
0
2

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?