• 締切済み

SmartyでMySQLに登録したテンプレートを使用するには?

現在、Smartyを使用してブログを作成しようとしています。 テンプレートファイル(htmlソース)をMySQLに登録し、 これを呼び出して出力する方法を教えてください。 ※各ユーザーでレイアウトを変更するため Smartyマニュアルの「テンプレートリソース」等を確認しましたが、 なかなかうまく表示されません。 http://smarty.php.net/manual/ja/template.resources.php MySQLに「template」フィールド作成し、ここにHTMLソースを記述しています。 試した方法は 1.display時に「template」フィールドを呼び出し ×変数が呼び出されない 2.custom.tplを作成し、「template」フィールドをecho。そしてdisplay ×変数がそのまま出力 3.「テンプレートリソース」の利用 ×うまく表示されない クラス「SQL」を作成しないといけない? サンプルや他に方法が記載されているサイトがあれば教えてください。

noname#129183
noname#129183
  • PHP
  • 回答数2
  • ありがとう数0

みんなの回答

回答No.2

こんにちは ちょうど、同じような事をやってました。 MySql用に試してみました。 記事にまとめましたので、参考にしてみてください。 http://okinawasanpo.ti-da.net/e1860324.html

参考URL:
http://okinawasanpo.ti-da.net/e1860324.html
  • tany180sx
  • ベストアンサー率63% (239/379)
回答No.1

毎回コンパイルされますが、evalという方法もあります。 {eval var=$html}

参考URL:
http://smarty.php.net/manual/ja/language.function.eval.php

関連するQ&A

  • smaryテンプレートを使う上で、Smartyオブジェクトを二つ作成し

    smaryテンプレートを使う上で、Smartyオブジェクトを二つ作成し、それぞれのそのなかの変数をひとつのテンプレートに表示するという方法は可能でしょうか?つまりそれぞれを同じテンプレートに表示する、という方法はできるのでしょうか?PHPは下記のようになります。 $smarty = new Smarty(); $smarty2 = new Smarty(); $smarty->assign('abc', $def); $smarty2->assign('ghi', $jkl); $smarty->display('aaa.tpl'); $smarty2->display('aaa.tpl'); それとも、オブジェクトと表示するテンプレートは1つずつでないといけないとだめで、$smartyと$smarty2は、どっちかひとつに統一する必要があるのでしょうか?

    • ベストアンサー
    • PHP
  • Smartyでテンプレートが読み込めない

    下記のソースを実行すると、テンプレートが読み込めずエラーになってしまいます。 うーん、何故なのか分からず困っております。 phpのバージョンは4.4.6なのですが、Ver5以降でないとサポートされないのでしょうか。 [source] <?php require_once("Smarty.class.php"); class MySmarty extends Smarty { function _MySmarty() { $this->Smarty(); $this->template_dir="../templates"; $this->compile_dir="../templates_c"; } } $o_smarty=new MySmarty(); $o_smarty->display("hello.tpl"); ?> [error message] Warning: Smarty error: unable to read resource: "hello.tpl"

    • ベストアンサー
    • PHP
  • smartyテンプレート読み込みについて

    Smarty動的Webサイト構築入門という書籍を見ながらSmartyを学習しているのですが、書籍のCD-ROMから落としたサンプルファイルを保存しているのですがどうしてもテンプレートを読み込むことができません。 03_01.php <?php require_once("../smarty/libs/Smarty.class.php"); $smarty = new Smarty(); $smarty->template_dir = "templates"; $smarty->compile_dir = "templates_c"; $smarty->assign("name", "Smartyさん"); $smarty->display("03_01.tpl"); ?> 03_01.tpl Hello, {$name}!! エラーメッセージ Warning: Smarty error: unable to read resource: "03_01.tpl" in C:\xampp\htdocs\smartybook\smarty\libs\Smarty.class.php on line 1092 03_01.phpは、C:\xampp/htdocs/smartybook/ren/に保存しています。 03_01.tplは、C:\xampp/htdocs/smartybook/templates/に保存しています。 どこが駄目なのか教えてください。 宜しくお願いします。

    • 締切済み
    • PHP
  • Smartyについて教えてください。

    Smartyについて教えてください。 参考書によってテンプレートの存在するディレクトリを指定している場合(1)と していない場合(2)がありますが、 どちらが正しいのでしょうか? (1) ********************************************* require_once('smarty/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = "template02/"; $smarty->compile_dir = "compile/"; $smarty->display("content.tpl"); ********************************************* (2) ********************************************* require_once('smarty/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->display("content.tpl"); *********************************************

    • ベストアンサー
    • PHP
  • Smarty includeでテンプレートを読み込めない

    よろしくお願いします。 まずphpファイルから $smarty->display('111/body.tpl');(*111はディレクトリ名) を呼び出して、さらにbody.tplと同ディレクトリにあるcontents.tplを読み込もうとしています。 はじめのbody.tplは読み込むことができブラウザで表示されるのですが、contents.tplは読み込めず、 Warning: Smarty error: unable to read resource: "contents.tpl" in <絶対パス>/Smarty/Smarty.class.php on line 1095 のエラーが出てしまいます。 ちなみに/111/というディレクトリは、$template_dir以下、$template_dir/111/です。 {include file="contents.tpl"} {include file="file:contents.tpl"} {include file="絶対パス/contents.tpl"} {include file="file:絶対パス/contents.tpl"} など試しましたが、ダメでした。 知恵をお貸しください。よろしくお願いします。

    • ベストアンサー
    • PHP
  • Smartyに関しまして

    require_once("Smarty/Smarty.class.php"); $smarty = new Smarty(); $smarty->template_dir = "./templates"; $smarty->compile_dir = "./templates_c"; $smarty->assign("message", "こんばんは"); $a = array('hello','php','html'); $smarty->assign('a',$a); $smarty->display("index.tpl"); これでindex.tplで出力すると日本語部分が「?????」になってしまいます。 何か設定がいるんでしょうか??

    • ベストアンサー
    • PHP
  • smartyで同じテンプレートを使いまわす

    list.tpl {foreach from=$data key=key item=row loop=test} <tr><td>{$row.name}</td>:<td>{$row.kokugo}</td><td>{$row.sansu}</td></tr> {/foreach} ----- index.tpl(一部) <h2>男の子</h2> {include file=list.tpl} <h2>女の子</h2> {include file=list.tpl} ----- index.php(一部) $Smarty -> assign( "data", $boy ); $Smarty -> assign( "data", $girl ); $Smarty -> display( 'index.tpl' ); (実際にこの書き方は間違っていますが…) index.php から index.tpl を display したいのですが、 この中で使うリストを、使いまわしたいと考えています。 こういうやり方はそもそも可能なのでしょうか? たしかに boylist.tpl 、 girllist.tpl とし中の割当変数を $boy 、 $girl に変えればいいのですが、 将来変更が加わった際に両方変えなければなりません。 今回は例なので男女ですが、もっと多くの要素が出て来た時に 厄介なことになるように思います。 何か解決方法はありませんでしょうか?

    • ベストアンサー
    • PHP
  • Smarty3のリソース・プラグイン

    Smarty3のリソース・プラグインについて Smarty3(3.0.9)でリソース・プラグインを行うと以下エラーが出力され、データベース内のテンプレートを呼び出すことができません。 Uncaught exception 'SmartyException' with message 'Unable to load template db 'test.tpl'' in /usr/local/lib/php/smarty/sysplugins/smarty_internal_template.php:165 Stack trace: #0 /usr/local/lib/php/smarty/sysplugins/smarty_internal_template.php(554): Smarty_Internal_Template->isExisting(true) #1 /usr/local/lib/php/smarty/Smarty.class.php(340): Smarty_Internal_Template->getRenderedTemplate() #2 /usr/local/lib/php/smarty/Smarty.class.php(384): Smarty->fetch('test.tpl', NULL, NULL, NULL, true) #3 /usr/local/apache2/htdocs/test.php(18): Smarty->display('test.tpl') #4 {main} thrown in /usr/local/lib/php/smarty/sysplugins/smarty_internal_template.php on line 165 同様のプログラムでSmarty2*で実行するとうまく表示します。 Smarty3のマニュアル(英語版)を見てもうまくいきません。。 ご存知の方、ぜひ教えてください。 (環境) OS:Debian 6.0.1 WebServer:Apache2.2.19 PHP:PHP5.3.6 MySQL:MySQL5.0.87 (プラグイン) <?php require('DB.php'); define("DB_FILE","mysqli://*****:*****@localhost/*****"); function smarty_resource_db_source ($name, &$source, $o_smarty) { $db = DB::connect(DB_FILE); $stt = $db->prepare("SELECT source FROM templates WHERE name =?"); $rs = $db->execute($stt,array($name)); if(is_null($row=$rs->fetchRow(DB_FETCHMODE_ASSOC))){ return FALSE; }else{ $source=$row["source"]; return TRUE; } } function smarty_resource_db_timestamp($name, &$modified, $o_smarty) { $db = DB::connect(DB_FILE); $stt = $db->prepare("SELECT * FROM templates WHERE name =?"); $rs = $db->execute($stt,array($name)); if(is_null($row=$rs->fetchRow(DB_FETCHMODE_ASSOC))){ return FALSE; }else{ $modified=$row["modified"]; return TRUE; } } function smarty_resource_db_secure($name, $o_smarty) { // 全てのテンプレートがセキュアであると仮定します return true; } function smarty_resource_db_trusted($name, $o_smarty) { // テンプレートから使用しません } ?> (呼び出しファイル) <?php set_include_path('/usr/local/lib/php/smarty/:' . get_include_path()); @include('Smarty.class.php'); $o_smarty = new Smarty(); // Smartyのテンプレートのキャッシュファイル格納先を指定 $o_smarty->compile_dir = "/usr/local/apache2/htdocs/template_c"; $o_smarty->plugins_dir[]="/usr/local/apache2/htdocs/plugin"; $o_smarty->default_resource_type="db"; // phpスクリプトからテンプレートリソースを使用します $o_smarty->display("test.tpl"); ?> (データベース) CREATE TABLE templates ( name VARCHAR(100) NOT NULL default '', modified TIMESTAMP, source TEXT, PRIMARY KEY (name) ) DEFAULT CHARACTER SET utf8; -- Dumping data for table 'templates' INSERT INTO templates VALUES('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');

    • 締切済み
    • PHP
  • ロリポップでSmartyのtpl読み込みエラー

    下記のコードを実行すると、  Smarty error: unable to read resource: "template1.tpl"  というエラーが出てしまいます。 $smarty->template_dirで指定したディレクトリ名の中に  "template1.tpl"というファイルは配置してあります。  原因などわかればおしえていただけないでしょうか。 index.php <?php require_once("Smarty.class.php"); $smarty = new Smarty; $smarty->template_dir = './templates/'; $smarty->compile_dir = './templates_c/'; $smarty->config_dir = './configs/'; $smarty->cache_dir = './cache/'; $smarty->display("template1.tpl"); ?> 【サーバ】  ロリポップです。 【その他】 index.phpをホームディレクトリ配下におくとエラーは消えます。 【ディレクトリ構成】 modules/index.php と templates/template1.tpl をがあり、 index.php から template1.tpl を読み込もうとしています。  ・modules →index.php ・templates →template1.tpl

    • ベストアンサー
    • PHP
  • Smartyのエラーが2つずつ出ます

    PHP5+Smartyでサイトを作っています。 「$smarty->display("hoge.tpl");」としてテンプレートファイルを呼び出します。 そのテンプレートファイル「hoge.tpl」が存在しない時に、以下のエラー文が必ず2つ表示されます。 Warning: Smarty error: unable to read resource: "hoge.tpl" in /root/.../smarty/Smarty.class.php on line 1092 一度しか呼び出していないのにエラーが2つ表示されるので、コードの記述ミスかと考えたのですがわかりませんでした。 テンプレートファイルがある場合は、テンプレートが二度呼び出されることはありません。 エラー文にある1092行目からコードを追って見たのですがわかりませんでした。 どうすればエラー表示を1つにできるのでしょうか? 宜しくお願いします。

    • 締切済み
    • PHP

専門家に質問してみよう