是否可以在jade / pug中编写PHP?

时间:2017-02-03 11:40:37

标签: php html pug

有可能吗?如果是这样,怎么样? 如果不是,如果我需要在我的文档中编写PHP,我是否必须放弃哈巴狗? 在四处寻找之后,我找不到任何对此有所了解的人。

6 个答案:

答案 0 :(得分:7)

You can embed PHP in Pug templates the same way you would any literal plain text that you want passed through relatively unmolested[*]. There are a number of options covered in the docs, but I think these are most likely the best options for embedding PHP:

  1. After an element, it will just work. For example, p Good morning, <?php echo $user->name ?>.
  2. On a single line by itself. Since any line beginning with "<" is passed as plain text, any one line PHP statement (e.g., <?php echo $foo; ?>) will just work.
  3. Multi-line PHP is the one case where it gets a bit complicated. If you're ok with wrapping it in an HTML element, you can use Pug's block text syntax: Put a dot after the element, then include your plain text indented underneath.

    p.
        <?php
        if ($multiline) {
            echo 'Foo!';
        }
        ?>
    

    If you need it outside an element, the other option is to prefix every line with a pipe:

    |<?php
    |if ($multiline) {
    |   echo 'Foo!';
    |}
    |?>
    

    (Technically, the first line doesn't need to be prefixed due to point 2 above, but if using this method I would prefix it anyway just for consistency.)

  4. To use PHP in attributes, you just need to prevent escaping by prefixing the equals sign with a bang: p(class!="<?php echo $foo ?>"). (Interestingly, support for unescaped attribute values was added specifically for this use case.)

Of course by default .pug files are compiled to .html files, so if you're using them to generate PHP, you'll want to change the extension. One easy way to do this is to process them using gulp with the gulp-pug and gulp-rename plugins, which would look something like this:

var gulp = require('gulp'),
     pug = require('gulp-pug'),
     rename = require('gulp-rename');

gulp.task('default', function () {
    return gulp.src('./*.pug')
        .pipe(pug())
        .pipe(rename({
            extname: '.php'
        }))
        .pipe(gulp.dest('.'));
});

I haven't worked extensively with Pug, so I don't know if there are any potential gotchas that would come up in real world use cases, but the simple examples above all work as expected.

[*] Pug still performs variable interpolation on plain text, but it uses the #{variable} format, which should not conflict with anything in PHP's standard syntax.

答案 1 :(得分:2)

由于PHP不关心“外部”代码是HTML还是任何特定的代码,您可以像往常一样使用PHP并输出Pug格式的代码而不是HTML。例如:

myPugTemplate.pug.php

html
  head
    title "<?= $this->title ?>"
  body
    <?php
      // Since we're outputing Pug markup, we have to take care of
      //   preserving indentation.
      $indent=str_repeat('  ', 2);
      if ($this->foo) {
        echo $indent . 'bar= myPost';
      } else {
        echo $indent . 'baz= myNav';
      }
    ?>
    footer
      +footerContent

如果您的Pug在服务器上处理,那么您还需要包含 Pug-processing 步骤,例如,如果您使用 Apache ,则可以使用 mod_ext_filter以这种方式配置pug-cli已安装:

ExtFilterDefine pug-to-html mode=output intype=text/pug outtype=text/html \
  cmd="pug"

<Location />
  SetOutputFilter pug-to-html
</Location>

答案 2 :(得分:1)

你检查过pug-php项目了吗?我个人没有这个特定模块的经验,但它似乎只是你要完成的事情:能够在Pug中使用PHP。

答案 3 :(得分:1)

您可以将scape语法与引号一起使用:

!{'<?php #php code  ?>'}

例如:

p Hello !{'<?php echo "My name"; ?>'}

将呈现:

<p>Hello <?php echo "My name"; ?></p>

您可以在此处进行测试:https://pug-demo.herokuapp.com/

答案 4 :(得分:0)

有一个众所周知的且维护良好的Pug处理器,它是用PHP原生编写的。您可以像使用原始Pug一样使用它将Pug文件处理为HTML,其优点是可以轻松地在Pug文件中嵌入和使用PHP代码。如果您在Pug中使用PHP,请检查一下:

Phug - the Pug template engine for PHP

答案 5 :(得分:0)

 p Hello !{'<?php echo "My name"; ?>'}

有效但

 link(href="../assets/css/style.css?v=!{'<?=$AppsVersion?>'}" rel="stylesheet" type="text/css")

不工作