我在Jekyll的Shopify Liquid for Loop时遇到了麻烦

发布于 2025-02-04 11:04:15 字数 4025 浏览 4 评论 0原文

以下代码与渲染,我还没有推动我的更改, 。因此,请记住这一点。

它应该

  • 通过YAML数据文件进行迭代。
  • 在所需的任何地方放置一个链接。

但是,目前,它仅在我想要的链接到处都是for循环看到的最后一个元素。

我有一个带有两个链接的YAML文件,而不是将链接放在与其PID关联的块中,而是将第二个链接放在所有链接位置中。

我正在使用Regex进行解析,因此在YAML文件中的任何位置,即aaaa在该位置更换​​链接。这是有效的,只是没有放置正确的链接。

现在,更令人困惑的一部分是第一个console.log console.log(faqlink {{faq_link_pid}});> prints prock prince off infile off conse.log > prince prince priongs and Orde ofer ofer offer。

<h1 class="fs-6 fw-500" align="center">Frequently Asked Questions <br> Welcome to our FAQ!</h1>
<div class="accordiobody">
    {% for faq in site.data.faq_yaml.frequently_asked_questions %}
    <div class="accordion">
        <hr>
        <div class="container">
            <div class="question fw-400">
                <h1 class="fs-5 fw-500">
                    {{ faq.question }}
                </h1>
            </div>
            <div class="answer">
                <blockquote>
                    <span class="answerspan">
                        {{ faq.answer }}
                    </span>
                </blockquote>
            </div>
        </div>
    </div>
    {% endfor %}
</div>
<script type="text/javascript">
    {% assign faq_link = "" %}
    {% assign faq_link_description = "" %}
    {% assign faq_link_pid = 0 %}
    {% for faq in site.data.faq_yaml.frequently_asked_questions %}
    {% if faq.hyper_link != null and faq.hyper_link != "" %}
    {% assign faq_link = faq.hyper_link %}
    {% assign faq_link_pid = faq.faq_link_pid %}
    

    const faqLink{{ faq_link_pid }} = "{{ faq_link }}";
    {% if faq.link_description != null and faq.link_description != "" %}
    {% assign faq_link_description = faq.link_description %}
    const faqLinkDescription{{ faq_link_pid }} = "{{ faq_link_description }}";
    console.log(faqLink{{ faq_link_pid }});
    function createElement() {
            for (let x in accordion) {
                const link{{ faq_link_pid }} = `<a href="${faqLink{{ faq_link_pid }}} " target="_blank">${faqLinkDescription{{ faq_link_pid }}}</a>`;
                console.log(link{{ faq_link_pid }});
                replaceLink(link{{ faq_link_pid }});
        }
    }
    {% endif %}
    window.addEventListener('load', function () {
        createElement();
    });
    {% endif %}
    {% endfor %}

    const accordion = document.getElementsByClassName('container');

    for (i = 0; i < accordion.length; i++) {
        accordion[i].addEventListener('click', function () {
            this.classList.toggle('active');
        })
    }

    function replaceLink(str) {
        const link = document.querySelectorAll('.answerspan');
        const all_link = link.forEach(function (link) {
            const hyper_link = link.innerHTML;
            link.innerHTML = hyper_link.replace(/aaaa./g, str);
        });
    }
</script>

YAML文件:

---
# Use this YAML file to create a list of FAQ questions and answers.

- question: "How will this work?"
  answer: "Currently, a camera is mounted inside the headset for each eye. The camera streams through wifi to a PC client which processes and sends eye-tracking data to VR Chat."
  hyper_link: ""
  link_description: ""
  faq_link_pid: 3

- question: "What features will be supported?"
  answer: "The goal is eye tracking with eye openness and some form of pupil dilation. A far away aspiration of this project is some form of weak foveated rendering because it's cool and any small performance increase in VR is welcome."
  hyper_link: ""
  link_description: ""
  faq_link_pid: 4

- question: "When will this be completed?"
  answer: "When it's done 
              

The below code is slightly different than what is rendered, I haven't pushed my changes yet. So keep that in mind.

It is supposed to

  • iterate through the YAML data file.
  • place a link everywhere that is required of it.

However, currently, it is only placing the last element seen by the for loop everywhere I want a link.

I have a YAML file with two links, and instead of placing the link in the block associated with its PID, it is placing the second link in all link locations.

I am using REGEX to parse, so anywhere in the YAML file that is aaaa a link gets replaced at that location. That works, it just doesn't place the right link.

Now the even more confusing part is that the first console.log console.log(faqLink{{ faq_link_pid }}); prints all of the links, correctly and in the order, they are in the file.

<h1 class="fs-6 fw-500" align="center">Frequently Asked Questions <br> Welcome to our FAQ!</h1>
<div class="accordiobody">
    {% for faq in site.data.faq_yaml.frequently_asked_questions %}
    <div class="accordion">
        <hr>
        <div class="container">
            <div class="question fw-400">
                <h1 class="fs-5 fw-500">
                    {{ faq.question }}
                </h1>
            </div>
            <div class="answer">
                <blockquote>
                    <span class="answerspan">
                        {{ faq.answer }}
                    </span>
                </blockquote>
            </div>
        </div>
    </div>
    {% endfor %}
</div>
<script type="text/javascript">
    {% assign faq_link = "" %}
    {% assign faq_link_description = "" %}
    {% assign faq_link_pid = 0 %}
    {% for faq in site.data.faq_yaml.frequently_asked_questions %}
    {% if faq.hyper_link != null and faq.hyper_link != "" %}
    {% assign faq_link = faq.hyper_link %}
    {% assign faq_link_pid = faq.faq_link_pid %}
    

    const faqLink{{ faq_link_pid }} = "{{ faq_link }}";
    {% if faq.link_description != null and faq.link_description != "" %}
    {% assign faq_link_description = faq.link_description %}
    const faqLinkDescription{{ faq_link_pid }} = "{{ faq_link_description }}";
    console.log(faqLink{{ faq_link_pid }});
    function createElement() {
            for (let x in accordion) {
                const link{{ faq_link_pid }} = `<a href="${faqLink{{ faq_link_pid }}} " target="_blank">${faqLinkDescription{{ faq_link_pid }}}</a>`;
                console.log(link{{ faq_link_pid }});
                replaceLink(link{{ faq_link_pid }});
        }
    }
    {% endif %}
    window.addEventListener('load', function () {
        createElement();
    });
    {% endif %}
    {% endfor %}

    const accordion = document.getElementsByClassName('container');

    for (i = 0; i < accordion.length; i++) {
        accordion[i].addEventListener('click', function () {
            this.classList.toggle('active');
        })
    }

    function replaceLink(str) {
        const link = document.querySelectorAll('.answerspan');
        const all_link = link.forEach(function (link) {
            const hyper_link = link.innerHTML;
            link.innerHTML = hyper_link.replace(/aaaa./g, str);
        });
    }
</script>

The YAML file:

---
# Use this YAML file to create a list of FAQ questions and answers.

- question: "How will this work?"
  answer: "Currently, a camera is mounted inside the headset for each eye. The camera streams through wifi to a PC client which processes and sends eye-tracking data to VR Chat."
  hyper_link: ""
  link_description: ""
  faq_link_pid: 3

- question: "What features will be supported?"
  answer: "The goal is eye tracking with eye openness and some form of pupil dilation. A far away aspiration of this project is some form of weak foveated rendering because it's cool and any small performance increase in VR is welcome."
  hyper_link: ""
  link_description: ""
  faq_link_pid: 4

- question: "When will this be completed?"
  answer: "When it's done ???? I have a semi-busy life so development may slow and speed up inconsistently. I have a goal to be done with all base features in June."
  hyper_link: ""
  link_description: ""
  faq_link_pid: 5

- question: "Will IR damage my eyes?"
  answer: "This project has safety in mind. If you do all of the safety measures we put in place and visually test the amount of IR light you will be fine. Please note we have not finished development of all safety stuff so be careful aaaaa  ."
  hyper_link: "https://dammedia.osram.info/media/bin/osram-dam-2496608/AN002_Details%20on%20photobiological%20safety%20of%20LED%20light%20sources.pdf"
  link_description: "here is a pdf with safety information"
  faq_link_pid: 6

- question: "How expensive will this be?"
  answer: "My goal is to keep it as cheap as possible with around $75 as the absolute max, with current projections being around $25-40"
  hyper_link: ""
  link_description: ""
  faq_link_pid: 7

- question: "How do I set up my avatar?"
  answer: "Check out the VR Chat face tracking wiki on our GitHub. Keep in mind that we currently only support float parameters. aaaa "
  hyper_link: "https://google.com"
  link_description: "Google"
  faq_link_pid: 8
---

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2025-02-11 11:04:15

您为什么要使用Regex + JavaScript放置链接?最好只将所有您想要的所有隐藏在html中的所有内容都放在&lt; div class = hidden = hidden”&gt;中,然后切换css属性display> display> display> none for div使用DIV使用JavaScript

也可以使用JS的浏览器更加专门:在HTML中写下所有内容,并将显示器留在您想要的Divs上(即不要在CSS文件上设置任何显示)。在页面上加载一个显示:所有这些div元素中都使用JS来切换单个元素的

显示

jQuery(document).ready(function($) {
  //Set default open/close settings
  var divs = $('.hidden').hide(); //Hide/close all containers

。下一个元素(您必须查看您的DOM树,以确切获取哪个元素)

Why would you use regex + javascript to place the link? It's best to just put everything you want hidden in the HTML inside a <DIV class=hidden"> and then toggle a CSS property display:none for the DIV using javascript.

This would also work better with browsers with js turned off. More specifically: write everything in the HTML and leave the display on the DIVs you want hidden (i.e. do not set any display on the CSS file). Then use js to place upon the page loading a display:none in all these DIV elements; then use js to toggle the display for a single element.

E.g. using jquery, you would do something like

jQuery(document).ready(function($) {
  //Set default open/close settings
  var divs = $('.hidden').hide(); //Hide/close all containers

and then on the clickable element use slideToggle() to toggle the display of the next element (you will have to look at your DOM tree to get exactly which element)

皓月长歌 2025-02-11 11:04:15

我通过探索所有JavaScript和Regex并简单地使用Liquid 替换函数来解决我的问题。不需要JS。我只是对此过于复杂,因为我还没有足够好阅读液体文档结构:)


<span class="answerspan{{ faq.faq_link_pid }}">
  {%- capture editable_part -%}
   <a href="{{ faq.hyper_link }}" target="_blank">{{ faq.link_description }}</a>
  {%- endcapture -%}
  {% if faq.answer contains 'aaaa ' %}
  {{ faq.answer | replace: 'aaaa ', editable_part }}
  {% else %}
  {{ faq.answer }}
  {% endif %}
</span>

I solved my issue by delting all of the javascript and REGEx and simply using the Liquid replace function. No JS needed. I was just over-complicating it because i hadn't read the liquid documetnation well enough :)


<span class="answerspan{{ faq.faq_link_pid }}">
  {%- capture editable_part -%}
   <a href="{{ faq.hyper_link }}" target="_blank">{{ faq.link_description }}</a>
  {%- endcapture -%}
  {% if faq.answer contains 'aaaa ' %}
  {{ faq.answer | replace: 'aaaa ', editable_part }}
  {% else %}
  {{ faq.answer }}
  {% endif %}
</span>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文