LoginSignup
1
0

More than 3 years have passed since last update.

Pug内にPHP記述する

Posted at

HTMLのテンプレートエンジンであるPugとPHPとを共存させる方法について書き留めておきます。

. で直接PHPを記述する

Pugの仕様でピリオド(.)の後のインデントの中のテキストはコンパイルされず、そのまま出力されます。
それを利用します。

index.pug

.
    <?php 

    require("configfile.php");

    ?>


html
    head
        meta(http-equiv="X-UA-Compatible" content="IE=edge")
        title
        meta(charset="utf-8")
        meta(name="description" content="")
        meta(name="author" content="")
        meta(name="viewport" content="width=device-width, initial-scale=1")
        link(rel="stylesheet" href=path_css+"style.css")
        link(rel="shortcut icon" href="")

        script(src=path_js+"model.js") 

属性値などでPHPを利用する

クラス名などの属性値では、上述の書き方は利用できませんので、属性値に直接PHPを記述します。このとき属性値内の"<"と">"がコンパイルされないように 属性 != "属性値" の形で =の前に!をつけることでエスケープさせます。

index.pug
section(class!="<= $class_name?>")
1
0
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
1
0