首頁  >  文章  >  php框架  >  yii2怎麼將其他範本引入內容範本頁面

yii2怎麼將其他範本引入內容範本頁面

王林
王林原創
2020-02-17 14:31:421939瀏覽

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>

(相關教學建議:yii框架

使用:

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

進行引入,要注意的是,在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>

更多程式設計相關內容學習,請追蹤php中文網程式教學欄位!

以上是yii2怎麼將其他範本引入內容範本頁面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn