Magento2ページがホームページの場合はテンプレートでチェックインしてください
-
29-09-2020 - |
質問
要求されたページがホームページの場合、.phtmlテンプレートで直接チェックする方法は?
特定のコンテナにクラスを条件付きで追加したいと思います
解決
どのページがテンプレートからのホームページであるかをチェックしている場合は、Magentoテンプレートとレイアウトに誤って近づいています。
私のアプローチは一般的に core / template のブロックを作成し、正しいレイアウトハンドルにレイアウトを介して追加することです。 。
この回答に詳しく説明できます。
https://magento.stackexchange.com/a/30562/336
この誤解の概念の詳細は私のブログにここで見つけることができます:
https://blog.philwinkle.com/the-ほとんどの誤解概念 - in-magento /
Magentoレイアウトにさらに深いダイビングのためにトピックでAlan Stormの本を読む:
href="http://store.pulsestorm.net/products/no-frofers-magento-layout" REL="nofollowNoreferrer"> http://store.pulsestorm.net/products/no-fillls. - マゲンテレイアウト
他のヒント
クラスコンストラクタで\ Magento \ Framework \ App \ Request \ HTTPのインスタンスを使用できます。コントローラにいる場合は、それをする必要はありません。
この$request = $this->getRequest()
public function __construct(
...
\Magento\Framework\App\Request\Http $request
) {
...
$this->_request = $request;
}
.
その後、このようなホームページやカテゴリページ、またはプロダクトページがあるかどうかを確認できます。
if ($this->_request->getFullActionName() == 'cms_index_index') {
//you are on the homepage
}
if ($this->_request->getFullActionName() == 'catalog_product_view') {
//you are on the product page
}
if ($this->_request->getFullActionName() == 'catalog_category_view') {
//you are on the category page
}
.
オブジェクトマネージャを使用してPHTMLファイルで直接使用されています
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('\Magento\Framework\App\Request\Http');
.