LoginSignup
14
14

More than 5 years have passed since last update.

共通のテンプレートにアクション毎に設定できる追加ファイルを読み込ませる

Posted at

目的:共通のテンプレートを使用している状態で、アクション毎に微修正を加えたい。 例えば、jsファイルの読み込み等

手順

1. テンプレートにアクション毎に設定できるようAssetを仕込む

  今回はjsファイルをアクション毎に読み込ませたいので、jsグループを設定し、テンプレートでrender('js')し、出力する

使用するテンプレート (アクション毎に読み込ませるjsファイルを変えられるようにする)

/fuel/app/views/template.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <header>
        <?php echo $header; ?>
    </header>
    <div id="content">
        <?php echo $content; ?>
    </div>

    <footer>
        <?php echo $footer; ?>
    </footer>
    <?php
    //</body>の直前にjsグループで定義したjsファイルを読み込ませる。このファイルは各アクション毎に設定
    //jsグループのjsファイルを表示
    echo Asset::render('js');
    ?>
</body>
</html>

2. アクションのviewファイルでjsグループの設定をする。 (読み込ませたいファイルを指定する)

アクションのview これがテンプレートのcontentに渡される。 ここで、jsグループを設定する

/fuel/app/views/hello/index.php
<div>
    コンテンツ領域です
</div>

<?php
//テンプレートの</body>タグ直前に読み込ませたいファイルを指定。グループはjs
//test.js, hello.jsは/public/assets/js/配下に置く
Asset::js(array('test.js', 'hello.js'), array(), 'js', false);
?>

結果
下記がfooterタグの下に表示される

    <script type="text/javascript" src="http://localhost/fuelphp/assets/js/test.js?1398130262"></script>
    <script type="text/javascript" src="http://localhost/fuelphp/assets/js/hello.js?1398130262"></script>

公式サイト
Asset

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