簡體   English   中英

如何在 html 頁面中動態顯示 php 代碼

[英]How to display php code in html page dynamically

拜托,我有使用條款頁面,我想使用 laravel 和這樣的商店名稱播種;

'<p>Welcome to <?php printf(core()->getCurrentChannel()->business_name) ?>. We reserve the right... </p>

所以我希望它顯示為:Welcome to ufanstore。 我們保留權利...

如果管理員重命名他/她的商店名稱,則商店名稱可以更改。 我的問題是如何顯示它以便它可以動態更改?

Laravel 使用 php 刀片模板,因此如果您想將來自數據庫的代碼僅打印為文本,則可以使用此方括號{{ }} 如果您有多行文本或數據庫中有 html 代碼並且您想打印它,您可以使用{!! !!} {!! !!}在您的示例中,您可以使用以下代碼

<p>Welcome to {{core()->getCurrentChannel()->business_name}} . We reserve the right... </p>

如果您想在本機 php 代碼中打印它,請使用:

<?= core()->getCurrentChannel()->business_name ?>

注意:確保您當前從 controller 獲取數據,如果它沒有獲取數據,則使用print_r()將來自數據庫的打印數據打印為print_r(core()->getCurrentChannel())如果它沒有獲取數據,請再次重新檢查您的代碼。

萬一有人需要,我用以下方法解決了我的問題;

我將我的 html 代碼播種如下<p>Welcome to {Name}. We reserve the right to update or modify these Terms of Use at any time without prior notice</p> <p>Welcome to {Name}. We reserve the right to update or modify these Terms of Use at any time without prior notice</p>

在我的 controller 我執行此操作$pageContent = str_replace('{Name}', core()->getCurrentChannel()->business_name, $page->page_content);

然后在我的刀片中,我可以使用 { $pageContent $頁面內容!!}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM