Rumah > rangka kerja php > YII > yii如何引用模板

yii如何引用模板

藏色散人
Lepaskan: 2020-01-09 10:09:03
asal
2002 orang telah melayarinya

yii如何引用模板

yii如何引用模板?

在yii2中内容模板页面引入其他模板的方法

推荐学习:yii框架

在yii2中内容模板页面引入其他模板的方法

 

在view文件中,如user.php的view文件。

<?php
defined(&#39;YII_ENV&#39;) or exit(&#39;Access Denied&#39;);
 
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/27
 * Time: 11:18
 */
 
use yii\widgets\LinkPager;
 
$urlManager = Yii::$app->urlManager;
$this->title = &#39;业务员列表&#39;;
$this->params[&#39;active_nav_group&#39;] = 2;
?>
 
<div class="panel mb-3">
    <div class="panel-header">
        <span><?= $this->title ?></span>
        <ul class="nav nav-right">
            <li class="nav-item">
                <a class="nav-link" href="<?= $urlManager->createUrl([&#39;mch/salesman/salesman-edit&#39;]) ?>">添加业务员</a>
            </li>
        </ul>
    </div>
    <div class="panel-body">
        <table class="table table-bordered bg-white">
            <thead>
            <tr>
                <th>ID</th>
                <th>手机</th>
                <th>姓名</th>
                <th>绑定用户</th>
                <th>修改时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($list as $index => $val) : ?>
                <tr class="nav-item1">
                    <td>
                        <span><?= $val[&#39;id&#39;]?></span>              
                    </td>
                    <td><?= $val[&#39;mobile&#39;] ?></td>
                    <td><?= $val[&#39;truename&#39;] ?></td>
                    <td><?= $val[&#39;user_id&#39;];?></td>
                     <td><?= Yii::$app->formatter->asDatetime($val[&#39;edittime&#39;],"Y-M-d H:m");?></td>
                    <td>
                        <a class="btn btn-sm btn-primary"
                           href="<?= $urlManager->createUrl([&#39;mch/salesman/salesman-edit&#39;, &#39;id&#39; => $val[&#39;id&#39;]]) ?>">修改</a>
                        <a class="btn btn-sm btn-danger del"
                           href="<?= $urlManager->createUrl([&#39;mch/salesman/salesman-del&#39;, &#39;id&#39; => $val[&#39;id&#39;]]) ?>">删除</a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php echo $this->render(&#39;@app/views/layouts/paginator.php&#39;,[&#39;pagination&#39;=>$pagination]);?>
    </div>
</div>
<script>
    $(document).on(&#39;click&#39;, &#39;.nav-item1&#39;, function () {
        if($(this).find(".trans")[0].style.display==&#39;inline-block&#39;){
            $(this).find(".trans")[0].style.display=&#39;inline&#39;;
        }else{
            $(this).find(".trans")[0].style.display=&#39;inline-block&#39;;
        }
        $(&#39;.bg-&#39;+$(this).index(".nav-item1")).toggle();
    }); 
    $(document).on(&#39;click&#39;, &#39;.del&#39;, function () {
        if (confirm("是否删除该记录,删除后不可恢复?")) {
            $.ajax({
                url: $(this).attr(&#39;href&#39;),
                type: &#39;get&#39;,
                dataType: &#39;json&#39;,
                success: function (res) {
                    alert(res.msg);
                    if (res.code == 0) {
                        window.location.reload();
                    }
                }
            });
        } 
        return false;
    });
</script>
Salin selepas log masuk

使用

<?php echo $this->render(&#39;@app/views/layouts/paginator.php&#39;,[&#39;pagination&#39;=>$pagination]);?>
Salin selepas log masuk

进行引入,要注意的是,在render前使用输出语句echo,显示子模板内容,参数的使用同在action中,@app模板变量代表主文件夹。

子模板代码如下:

<?php use yii\widgets\LinkPager;?>
<div class="text-center">
<nav aria-label="Page navigation example">
        <?php
            echo LinkPager::widget([
            &#39;pagination&#39; => $pagination,
            &#39;prevPageLabel&#39; => &#39;上一页&#39;,
            &#39;nextPageLabel&#39; => &#39;下一页&#39;,
            &#39;firstPageLabel&#39; => &#39;首页&#39;,
            &#39;lastPageLabel&#39; => &#39;尾页&#39;,
            &#39;maxButtonCount&#39; => 5,
            &#39;options&#39; => [
                &#39;class&#39; => &#39;pagination&#39;
            ],
            &#39;prevPageCssClass&#39; => &#39;page-item&#39;,
            &#39;pageCssClass&#39; => "page-item",
            &#39;nextPageCssClass&#39; => &#39;page-item&#39;,
            &#39;firstPageCssClass&#39; => &#39;page-item&#39;,
            &#39;lastPageCssClass&#39; => &#39;page-item&#39;,
            &#39;linkOptions&#39; => [
                &#39;class&#39; => &#39;page-link&#39;
            ],
            &#39;disabledListItemSubTagOptions&#39; => [
                &#39;tag&#39; => &#39;a&#39;,
                &#39;class&#39; => &#39;page-link&#39;
            ]
        ])?>
    </nav>
<div class="text-muted">共<?= $pagination->totalCount ?>条数据</div>
</div>
Salin selepas log masuk

Atas ialah kandungan terperinci yii如何引用模板. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
yii
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan