【问题标题】:Add a class to a Shopify liquid link?向 Shopify 流动链接添加课程?
【发布时间】:2018-01-30 20:39:59
【问题描述】:

我正在尝试为我的客户生成一个注销链接,但想为该链接应用一个类。

{{ 'layout.customer.log_out' | t | customer_logout_link }}

以上液体代码生成

<a href="/account/logout" id="customer_logout_link">Log out</a>

我想添加一个类属性。例如,

<a href="/account/logout" class="CLASS-NAME" id="customer_logout_link">Log out</a>

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    您可以使用replace 过滤器将类添加到链接,您的代码将如下所示

    {{ 'layout.customer.log_out' | t | customer_logout_link | replace: '<a', '<a class="my_class"' }}
    

    【讨论】:

    • 我宁愿选择这个而不是解决方案,因为类的样式比 id 更好
    • 这是更好的答案,因为您希望这些链接是动态的。否则为什么要使用液体标签?
    【解决方案2】:

    您不能直接将类添加到链接过滤器,但您可以添加自己的链接。

    所以下面的代码{{ 'layout.customer.log_out' | t | customer_logout_link }}会被转换成。

    &lt;a href="/account/logout" id="customer_logout_link"&gt;{{ 'layout.customer.log_out' | t }}&lt;/a&gt;

    你可以添加任何你喜欢的课程。

    过滤器customer_logout_link 只是编写标准链接的简写。如果您打算使用按钮的标准 HTML 结构之外的任何内容,只需将其写为标准 html 链接即可。

    【讨论】:

      【解决方案3】:

      为了两全其美,您仍然可以利用动态 url 并将其拆分为您自己的 html,方法是在液体中添加 route 作为hrefSee Shopify documentation on routes

      <a href="{{ routes.account_logout_url }}" class="my_class" >
        {{ 'layout.customer.log_out' | t }}
      </a>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-14
        • 1970-01-01
        • 2023-04-07
        • 2018-09-24
        • 1970-01-01
        • 1970-01-01
        • 2018-08-03
        相关资源
        最近更新 更多