Liquid: successive random numbers excluding the first results - shopify

The purpose of the code is to ask random security questions using Liquid. I used the following code which creates a random number which is then replaced by a question:
{% assign min = 1 %}
{% assign max = 7 %}
{% assign diff = max | minus: min %}
{% assign randomNumber1 = "now" | date: "%N" | modulo: diff | plus: min %}
{{randomNumber1 | replace: "1", "Question 1" | replace: "2", "Question 2" | replace: "3", "Question 3" | replace: "4", "Question 4"| replace: "5", "Question 5"| replace: "6", "-Question 6"| replace: "7", "Question 7"}}
I copied this code three times to have three numbers/questions (with randomNumber1, randomNumber2, randomNumber3). The tricky part is that a question should not appear twice.
For that randomNumber2 must never be equal to randomNumber1 and randomNumber3 must never be equal to randomNumber1 and randomNumber2. Unfortunately I have not found an ideal solution to code these conditions.
Do you have one maybe? Thanks :)

Related

Multi-line regex only matches if begin & end are on the same line

I'm trying to add a bit of grammar for the Liquid language to my custom extension. Here is the bit of code in question:
{% if type == "record"
or type == "record2"
%}
records
{% else %}
no_records
{% endif %}
Then I have the following rule in my language repository:
"liquid_tags": {
"name": "source.qqql.liquid",
"begin": "\\{(\\%)",
"end": "(\\%)\\}",
"beginCaptures": {
"1": {"name": "source.qqql.liquid.tag"}
},
"endCaptures": {
"1": {"name": "source.qqql.liquid.tag"}
}
},
The problem is that the %} of the if statement is not captured. Only the other closing tags are captured because they're on the same line.

Splunk : Extracting the elements from JSON structure as separate fields

In Splunk, I'm trying to extract the key value pairs inside that "tags" element of the JSON structure so each one of the become a separate column so I can search through them.
for example :
| spath data | rename data.tags.EmailAddress AS Email
This does not help though and Email field comes as empty.I'm trying to do this for all the tags. Any thoughts/pointers?
{
"timestamp": "2021-10-26T18:23:05.180707Z",
"data": {
"tags": [
{
"key": "Email",
"value": "john.doe#example.com"
},
{
"key": "ProjectCode",
"value": "ABCD"
},
{
"key": "Owner",
"value": "John Doe"
}
]
},
"field1": "random1",
"field2": "random2"
}
I think does what you want:
| spath data.tags{}
| mvexpand data.tags{}
| spath input=data.tags{}
| table key value
| transpose header_field=key
| fields - column
How it works:
| spath data.tags{} takes the json and creates a multi value field that contains each item in the tags array
| mvexpand data.tags{} splits the multi value field into individual events - each one contains one of the items in the tags array
| spath input=data.tags{} takes the json in each event and makes a field for each KVP in that item (key and value in this case)
| table key value limits further commands to these two fields
| transpose header_field=key makes a field for each value of the key field (including one for the field named column)`
| fields - column removes the column field from the output
Here is a fully runnable example:
| makeresults
| eval _raw="
{
\"timestamp\": \"2021-10-26T18:23:05.180707Z\",
\"data\": {
\"tags\": [
{\"key\": \"Email\", \"value\": \"john.doe#example.com\"},
{\"key\": \"ProjectCode\", \"value\": \"ABCD\"},
{\"key\": \"Owner\", \"value\": \"John Doe\"}
]
},
\"field1\": \"random1\",
\"field2\": \"random2\"
}
"
| spath data.tags{}
| mvexpand data.tags{}
| spath input=data.tags{}
| table key value
| transpose header_field=key
It creates this output:
+----------------------+-------------+----------+
| Email | ProjectCode | Owner |
+----------------------+-------------+----------+
| john.doe#example.com | ABCD | John Doe |
+----------------------+-------------+----------+

Accessing nested values from YAML data in a Twig template

I am trying to access nested YML data in a Twig template. My YML data is structured like this:
card_default:
card_title: 'Card Title'
card_header: 'This is the card header on a multi-part card'
card_text: "Some quick example text to build on the card title and make up the bulk of the card's content."
card_link: 'https://github.com/phase2/particle'
card_background: primary
card_image_location: top
card_footer: "This is the card footer"
text_color: uk-dark
card_body: "This is some card body text"
card_style: default
card_image:
card_more_stuff in here....
... and then I call some of the data in a Twig template like this:
{% include '#molecules/card/_card.twig' with {
card_default: {
card_title: card_title,
card_text: card_text,
card_background: 'primary',
card_link: card_link,
card_link_text: card_link_text,
card_link_class: card_link_class,
}
} only %}
But that does not seem to work. I have a feeling the way I am trying to do this is not quite right but a search didn't give me any more insight. Essentially I want to access the values within card_default.
I can see all the data in the array if I dump with {{ dump(card_default) }}
array(14) { ["card_title"]=> string(10) "Card Title" ["card_header"]=> string(44) "This is the card header on a multi-part card" ["card_text"]=> string(94) "Some quick example text to build on the card title and make up the bulk of the card's content." ["card_link"]=> string(34) "https://github.com/phase2/particle" ["card_link_text"]=> string(9) "Read more" ["card_link_class"]=> string(27) "uk-button uk-button-default" ["card_background"]=> string(7) "primary" ["card_width"]=> int(25) ["card_image_location"]=> string(3) "top" ["card_footer"]=> string(23) "This is the card footer" ["list"]=> array(2) { ["list_flush"]=> bool(true) ["items"]=> array(3) { [0]=> array(1) { ["item_text"]=> string(15) "Cras justo odio" } [1]=> array(1) { ["item_text"]=> string(23) "Dapibus ac facilisis in" } [2]=> array(1) { ["item_text"]=> string(18) "Vestibulum at eros" } } } ["text_color"]=> string(7) "uk-dark" ["card_body"]=> string(27) "This is some card body text" ["card_style"]=> string(7) "default" }
The data is in the variable card_default, so it should be e.g. card_default.card_title
but instead of creating a whole new object you just could do this in 2 ways:
YAML
foo:
bar: 'foobar'
number: 42
main.twig
{% include "full.twig" with { 'foo' : foo } only %}
{% include "slim.twig" with foo only %}
full.twig
{{ foo.bar }}
slim.twig
{{ number }}
I figured this out, I just needed to map the nested items properly like so:
{% include '#molecules/card/_card.twig' with {
card_title: card_default.card_title,
card_text: card_default.card_text,
card_background: 'primary',
card_link: card_default.card_link,
card_link_text: card_default.card_link_text,
card_link_class: card_default.card_link_class,
} %}
In the above code, card_default is mapped in the variable portion of the array, i.e., after the colon. card_link: card_default.card_link,

how to properly parse paired html tags?

the question is about parsing an html stream obtained by load/markup in a way you can get html tags constituent parts, i.e. when you find
<div id="one">my text</div>
you should end with something like <div id="one">, {my text} and </div> in the same container, something like
[<div id="one"> {my text} </div>]
or even better
[<div> [id {one}] {my text} </div>]
the parsing problem is matching paired html tags, in html a tag may be an empty tag with maybe attributes but without content and thus without an ending tag or a normal tag maybe with attributes and content and so an ending tag, but both types of tag are just a tag
I mean when you find a sequence like <p>some words</p> you have a P tag just the same you get whit a sequence like <p /> just a P tag, in first case you have associated text and ending tag and in the latter you don't, that's all
In other words, html attributes and content are properties of tag element in html, so representing this in json you will get someting like:
tag: { name: "div" attributes: { id: "one } content: "my text" }
this means you have to identify content of a tag in order to assign it to properly tag, which in terms of rebol parse means identifing matching tags (opening tag and ending tag)
In rebol you can easy parse an html sequence like:
<div id="yo">yeah!</div><br/>
with the rule:
[ some [ tag! string! tag! | tag! ]]
but with this rule you will match the html
<div id="yo">yeah!</div><br/>
and also
<div id="yo">yeah!</p><br/>
as being the same
so you need a way to match the same opening tag when appearing in ending position
sadly rebol tags cannot (AFAIK) be parametrized with tag name, so you cannot say something like:
[ some [ set t1 tag! set s string! set t2 tag!#t1/1 | tag! ] ]
the t1/1 notation is due to a (bad) feature of rebol including all tag atributes at same level of tag name (another bad feature is not reckognizing matching tags as being the same tag)
Of course you can achieve the goal using code such as:
tags: copy []
html: {<div id="yo">yeah!</p><br/>}
parse html [ some [ set t1 tag! set s string! set t2 tag! (tag: first make block! t1 if none <> find t2 tag [append/only tags reduce [t1 s] ]) | tag! (append/only tags reduce [t1])]]
but the idea is to use a more elegant and naive approach using parse dialect only
There's a way to parse pairs of items in rebol parse dialect, simply using a word to store the expected pair:
parse ["a" "a"] [some [set s string! s ]]
parse ["a" "a" "b" "b"] [some [set s string! s ]]
But this doesn't work well with tags due to tags carry attributes and special ending marks (/) and thus it's not easy to find the ending pair from initial one:
parse [<p> "some text" </p>] [some [ set t tag! set s string! t ]
parse [<div id="d1"> "some text" </div>] [some [ set t tag! set s string! t ]
don't work cause </p> is not equal to <p> and neither </div> is equal to <div id="d1">
Again you can fix it with code:
parse load/markup "<p>preug</p>something<br />" [
some [
set t tag! (
b: copy t remove/part find b " " tail b
insert b "/"
)
set s string!
b (print [t s b])
|
tag!
|
string!
]
]
but this is not simple and zen code anymore, so question's still alive ;-)

Content for textarea will not fill when using volt syntax

Using the following code in my volt file, taken straight from the documentation (present here: http://docs.phalconphp.com/en/latest/reference/tags.html#helpers-to-generate-form-elements).
{{ text_area("comment", "This is the content", "cols": "6", "rows": 20) }}
It shows a textarea with correct name and id, correct column and rows but no content.
Only the first parameter is internally mapped to be the id.
All other parameters need a key.
This works:
{{ text_area("comment", "value":"This is the content", "cols": "6", "rows": 20) }}