是否可以在jade/pug中编写PHP? [英] Is it possible to write PHP in jade/pug?

查看:81
本文介绍了是否可以在jade/pug中编写PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能吗?如果是这样,怎么办? 如果不是,如果我需要在文档中编写PHP,是否必须放弃哈巴狗? 在四处搜寻之后,我找不到找到解决这个问题的人.

Is it possible? If so, how? If its not, do I have to abandon pug if I need to write PHP in my documents? After searching around I didnt find anyone that has adressed this.

推荐答案

您可以将PHP嵌入到Pug模板中,就像您要通过相对不干扰的[*]传递任何文字纯文本一样.有文档中包含许多选项,但我认为这些很可能是嵌入PHP的最佳选择:

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. 在元素之后,它将起作用.例如,p Good morning, <?php echo $user->name ?>.
  2. 单独一行.由于任何以<"开头的行以纯文本形式传递,则任何一行PHP语句(例如<?php echo $foo; ?>)都将起作用.
  3. 多行PHP有点复杂.如果可以将其包装在HTML元素中,则可以使用Pug的块文本语法:在该元素后放置一个点,然后在其下方包括缩进的纯文本.

  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!';
|}
|?>

(技术上,由于上​​面的第2点,第一行不需要加前缀,但是如果使用此方法,我还是会为保持一致性而加前缀.)

(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.)

当然,默认情况下,.pug文件会编译为.html文件,因此,如果使用它们生成PHP,则需要更改扩展名.一种简单的方法是使用 gulp gulp-rename 插件,如下所示:

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('.'));
});

我还没有与Pug进行广泛的合作,所以我不知道在现实的用例中是否存在任何潜在的麻烦,但是最简单的示例首先可以正常工作.

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仍对纯文本执行变量插值,但使用#{variable}格式,该格式不应与PHP标准语法中的任何内容冲突.

[*] 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.

这篇关于是否可以在jade/pug中编写PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆