2017-03-27 196 views
1

我試圖輸出2個不同的東西。例如:如果標題大於0,則執行此操作。如果沒有,請執行此操作。Laravel if else語句

我使用的DomDocument & Laravel 5.4

在我的控制器:

$title = $dom->getElementsByTagName('title'); 

在頁面上輸出:

@foreach ($title as $node) 
@if(!$node > 0) 
    {{'No title'}} 
    @else 
    {{$node->nodeValue, PHP_EOL}} <br /> 
    @endif 
@endforeach 

的問題:如果有一個標題它顯示標題。如果沒有標題,則不顯示任何內容。我想顯示:「無標題」。

爲什麼不能正常工作?

+0

沒有標題替換{{「無標題」}} – Indra

回答

1

你應該這樣做:

@if (condition) 
    No title 
@else 

但我懷疑!$node > 0部分你想要做什麼。

這是更好地使用三元運算符:

@foreach ($title as $node) 
    {{ empty($node->nodeValue) ? '' : $node->nodeValue }} <br /> 
@endforeach 
+0

嘿,感謝您的回覆!我發現我正在測試我的項目的網站有標籤,但它是空的。所以我已經將空($ node)更改爲空($ node-> nodeValue),它的工作方式就像一個魅力!請更新您的答案,以便我可以將此答案標記爲正確。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/7557655/">itvba</a></span> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">@itvba我更新了代碼。 – <span class="text-secondary"> <small> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">謝謝阿列克謝:) – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/7557655/">itvba</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-ouccfrfp-bg.html" target="_blank" title="if else語句"> if else語句 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-nqofzmvs-baq.html" target="_blank" title="if else if else語句(JS)"> if else if else語句(JS) </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-arfiuzid-bbo.html" target="_blank" title="if/else語句"> if/else語句 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-gawtsztb-bcw.html" target="_blank" title="If else語句"> If else語句 </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-rtzxnjfz-kk.html" target="_blank" title="if-else語句"> if-else語句 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-fufionqx-bbz.html" target="_blank" title="If/else語句"> If/else語句 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-qubrzmnr-eu.html" target="_blank" title="jquery if else語句"> jquery if else語句 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-rzyuuxns-vn.html" target="_blank" title="Big if else語句"> Big if else語句 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-mtwhdari-vx.html" target="_blank" title="IF ELSE語句SQLite"> IF ELSE語句SQLite </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-ogxxqhzj-yx.html" target="_blank" title="Python if else語句"> Python if else語句 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-gauxakhg-bac.html" target="_blank" title="jQuery if/else語句"> jQuery if/else語句 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-muihwmbe-zr.html" target="_blank" title="PHP if/else語句"> PHP if/else語句 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-nxidtkfd-nv.html" target="_blank" title="「形」 - if-else語句"> 「形」 - if-else語句 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-zofdqyut-ss.html" target="_blank" title="的if else語句"> 的if else語句 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-ydylogka-ma.html" target="_blank" title="PHP IF ELSE語句"> PHP IF ELSE語句 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-hhhjyxjh-rz.html" target="_blank" title="PHP - if ... else ...語句"> PHP - if ... else ...語句 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-rvfjnhuq-ps.html" target="_blank" title="VHDL If/Else語句"> VHDL If/Else語句 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-muzvzriq-zm.html" target="_blank" title="jquery if else語句"> jquery if else語句 </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-wockhkyq-bbs.html" target="_blank" title="string.format if else語句"> string.format if else語句 </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-hbcegomm-bcm.html" target="_blank" title="hash.indexOf()if else語句"> hash.indexOf()if else語句 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-kkavpneb-bgr.html" target="_blank" title="Oracle IF/ELSE語句"> Oracle IF/ELSE語句 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-dziaatet-ob.html" target="_blank" title="JQuery Else if語句?"> JQuery Else if語句? </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-wkbzhfuj-hv.html" target="_blank" title="XSL if else語句"> XSL if else語句 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-zjftoblv-sn.html" target="_blank" title="Verilog if-else語句"> Verilog if-else語句 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-csigbvpc-ur.html" target="_blank" title="PHP if else語句"> PHP if else語句 </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-cieonnnf-bbu.html" target="_blank" title="c#if else語句"> c#if else語句 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-fvaquilo-bkk.html" target="_blank" title="PHP else/if語句"> PHP else/if語句 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-qbanucpv-bno.html" target="_blank" title="if和else語句"> if和else語句 </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-ckqaforg-g.html" target="_blank" title="Spinner if else語句"> Spinner if else語句 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-vkklijbr-na.html" target="_blank" title="IF ELSE語句php"> IF ELSE語句php </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-uazvoukg-tn.html" target="_blank" title="Python的正則表達式:從一個字符串"> Python的正則表達式:從一個字符串 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-bfrziydq-py.html" target="_blank" title="分享偏好來保存個人資料圖片"> 分享偏好來保存個人資料圖片 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-tgbqscms-qh.html" target="_blank" title="有沒有辦法編寫一個函數,使用HttpServletRequest獲取IP地址而不將其作爲參數傳遞?"> 有沒有辦法編寫一個函數,使用HttpServletRequest獲取IP地址而不將其作爲參數傳遞? </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-sqerijvi-qt.html" target="_blank" title="如何測試處理ImportErrors的代碼?"> 如何測試處理ImportErrors的代碼? </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-rxiccvgv-rc.html" target="_blank" title="矩陣包中的提取速度與常規矩陣類相比非常緩慢"> 矩陣包中的提取速度與常規矩陣類相比非常緩慢 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-oxftnqwz-ro.html" target="_blank" title="pics not in codeignitor"> pics not in codeignitor </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-bndlqtaj-rx.html" target="_blank" title="特殊按鈕形狀(稍微旋轉箭頭)"> 特殊按鈕形狀(稍微旋轉箭頭) </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-ahxwsfxs-pp.html" target="_blank" title="emacs elisp切換到緩衝區,並按照"> emacs elisp切換到緩衝區,並按照 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-ylozrydp-ou.html" target="_blank" title="正在返回什麼以及本文檔中描述的功能正在採取什麼措施?"> 正在返回什麼以及本文檔中描述的功能正在採取什麼措施? </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-eoouwcdf-pd.html" target="_blank" title="MySQL數據庫 - 字符集和歸類轉換爲utf8mb4和utf8mb4_unicode_ci?"> MySQL數據庫 - 字符集和歸類轉換爲utf8mb4和utf8mb4_unicode_ci? </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-ouccfrfp-bg.html" target="_blank" title="if else語句"> if else語句 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-nqofzmvs-baq.html" target="_blank" title="if else if else語句(JS)"> if else if else語句(JS) </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-arfiuzid-bbo.html" target="_blank" title="if/else語句"> if/else語句 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-gawtsztb-bcw.html" target="_blank" title="If else語句"> If else語句 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-rtzxnjfz-kk.html" target="_blank" title="if-else語句"> if-else語句 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-fufionqx-bbz.html" target="_blank" title="If/else語句"> If/else語句 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-qubrzmnr-eu.html" target="_blank" title="jquery if else語句"> jquery if else語句 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-rzyuuxns-vn.html" target="_blank" title="Big if else語句"> Big if else語句 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-mtwhdari-vx.html" target="_blank" title="IF ELSE語句SQLite"> IF ELSE語句SQLite </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-ogxxqhzj-yx.html" target="_blank" title="Python if else語句"> Python if else語句 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>