【问题标题】:prestashop: Show read more at product description if description longer than XXX number of charactersprestashop:如果描述超过 XXX 个字符,则在产品描述中显示更多信息
【发布时间】:2014-02-16 18:33:39
【问题描述】:

你们能推荐我一种方法吗?只有在描述只有一定数量的字符时,我才能让阅读更多按钮出现?

{if isset($product) && $product->description }
    <!-- full description -->
    <div id="idTab1"  style="overflow:hidden;height:250px;font-family:avantgarde-book;font-size:12px;line-height:14px;">{$product->description}</div>
    <input id="button" type="button" style="margin-top:5px;font-size:12px;color:white;font-family:avantgarde-book; width:120px;background:#4e3248;border:none;height:30px;border-radius:5px;" value="Mostrar +" onclick="showMore()"> 
            <input id="button2" type="button" style="margin-top:5px;display:none;font-size:12px;color:white;font-family:avantgarde-book; width:120px;background:#4e3248;border:none;height:30px;border-radius:5px;" value="Mostrar -" onclick="showLess()">

{/if}

到目前为止,我得到了这段代码,它工作正常,但即使描述太短,按钮也会出现,所以我想让条件仅在描述为 XXX 个字符长或更多时才显示...

【问题讨论】:

  • 这与 Java 有什么关系?
  • 旁注:节省一些带宽,将内联 css 放入 css 文件可能是个好主意
  • 您可以使用PHP中的strlen函数来查看产品描述长度。如果超过限制,您可以使用substr 剪切字符串并附加“阅读更多”按钮/链接。另见stackoverflow.com/questions/79960/…

标签: javascript php prestashop prestashop-1.5


【解决方案1】:

这很容易,因为 prestashop 使用 smarty。

假设您想将描述限制为 200 个字符,然后显示阅读更多按钮

您可以使用 count_characters。这是一个例子:

{if isset($product) && $product->description }
        {* full description *}
        <div id="idTab1"  style="overflow:hidden;height:250px;font-family:avantgarde-book;font-size:12px;line-height:14px;">{$product->description}</div>
        {if $product->description|count_characters:true > 200 }
            <input id="button" type="button" style="margin-top:5px;font-size:12px;color:white;font-family:avantgarde-book; width:120px;background:#4e3248;border:none;height:30px;border-radius:5px;" value="Mostrar +" onclick="showMore()"> 
            <input id="button2" type="button" style="margin-top:5px;display:none;font-size:12px;color:white;font-family:avantgarde-book; width:120px;background:#4e3248;border:none;height:30px;border-radius:5px;" value="Mostrar -" onclick="showLess()">
        {else}
            {* don't display anything *}
        {/if}

{/if}

您可以从这里阅读有关 count_characters 及其属性的信息

http://www.smarty.net/docsv2/en/language.modifier.count.characters.tpl

P.S 在 smarty 语法中将某些内容显示为注释的正确方法是在这些标签之间显示它 {* *}

BR的

(如果这对您有帮助,请不要忘记接受它:))

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多