Set a new Variable in Assemble (or combine two comparison helpers)? - html

is there a way to set a new variable in a partial in assemble (assemble.io)? Or is it possible to combine two comparison-helpers, for example:
{#is somevar "yes" || anothervar "no"}
There's HTML in my partial which should only be shown if one of two different variables is true. If there's only one variable, i only write {{#is somevar "yes"}}, but i need that #is-helper for both variables.
I haven't found a way in the assemble-docs so i tried another way. I tried to do two #is-Helper like:
{{#is somevar "yes"}}
// HTML
{{/is}}
{{#is anothervar "no"}}
// HTML (the same as above)
{{/is}}
But this is really redundant... so i tried to set a variable inside both #is, for example:
{{checkvar = 0}}
{{#is somevar "yes"}}
{{checkvar = 1}}
{{/is}}
{{#is anothervar "no"}}
{{checkvar = 1}}
{{/is}}
So i only have to check for one variable (checkvar).
BUT all this wont work... So is there a way to do this in assemble?
Greetings

This would be something common to any Handlebars templates and not specifically Assemble.
Assemble uses handlebars-helpers and we've recently upgraded to Handlebars 3.0 so you can use a combination of subexpressions with the or and is helpers:
{{#or (is somevar "yes") (is anothervar "no")}}
<div>somevar = yes or anothervar = no</div>
{{/or}}
Edit: This might not work since the is helper is a block helper. You might have to write your own helper to just return the value. Either that or change somevar and anothervar to be truthy values instead of yes and no. Then you can just use the or helper directly:
{{#or somevar anothervar}}
{{/or}}

Related

Can I include conditional logic in VS Code snippets?

I would like to write a snippet in VS Code that writes a "switch" expression (in Javascript), but one where I can define the number of cases.
Currently there is a snippet that produces the outline of a switch expression with 1 case, and allows you to tab into the condition, case name, and the code contained within.
I want to be able to type "switch5" ("5" being any number) and a switch with 5 cases to be created, where I can tab through the relevant code within.
I know the snippets are written in a JSON file, can I include such conditional logic in this, or is it not possible?
Thanks!
The short answer is that you cannot do that kind of thing in a standard vscode snippet because it cannot dynamically evaluate any input outside of its designated variables with some limited workarounds like I'll mention next.
You might - I and others have written answers on SO about his - type your various case values first and then trigger a snippet tat would transform them into a switch statement. It is sort of doing it backwords but it might be possible.
There are extensions, however, that do allow you to evaluate javascript right in a snippet or setting and output the result. macro-commander is one such extension. I'll show another simpler extension doing what you want: HyperSnips.
In your javascript.hsnips:
snippet `switch(\d)` "add number of cases to a switch statement" A
``
let numCases = Number(m[1]) // 'm' is an array of regex capture groups
let caseString = ''
if (numCases) { // if not 'switch0'
let tabStopNum = 1
caseString = `switch (\${${tabStopNum++}:key}) {\n`
for (let index = 0; index < m[1]; index++) {
caseString += `\tcase \${${tabStopNum++}:value}:\n\t\t\$${tabStopNum++}\n`
caseString += '\t\tbreak;\n\n'
}
caseString += '\tdefault:\n'
caseString += '\t\tbreak;\n}\n'
}
rv = `${caseString}` // return value
``
endsnippet
The trickiest part was getting the unknown number of tabstops to work correctly. This is how I did it:
\${${tabStopNum++}:key}
which will resolve to ${n:defaultValue} where n gets incremented every time a tabstop is inserted. And :defaultValue is an optional default value to that tabstop. If you don't need a defaultValue just use \$${tabStopNum++} there.
See https://stackoverflow.com/a/62562886/836330 for more info on how to set up HyperSnips.

Is there a way of manipulating a global variable you use as an argument for a function in lua?

I'm trying to write some code into a function so that it can be easily reused whereby a global value is increased.
The issue is that there are several different globals that can be manipulated, and I need the function to be able to handle each one.
I thought I could simply put the global variable into the argument in the function but it doesn't seem to work. Probably because lua creates a copy of the global variable it takes.
Is there a way to make sure that the global variable it manipulates is not just a copy?
I have already tried to make sure that all the relevant variables remain local, but the problem is that they are all in a loop, and so need to remain outside the loop to be able to increment properly.
Right now I have to write a separate function for each case of incrementation which is a little unweildy, if bareable.
Right now I have to do something like this
foo_time_1 = 0
foo_time_2 = 0
function foo_time_1(var)
foo_time_1 += 2
if foo_time < 2 then
sfx(02)
end
end
function foo_time_2(var)
if foo_time_2 < 2 then
sfx(02)
end
end
This allows the code to run as intended, but Ideally I would like the code to look something like this:
foo_time = 0
foo_time_2 = 0
function foo_time_manipulator(global_var, len, sfx)
global_var += 2
if global_var < len then
play(sfx)
end
end
//so I can write
foo_time_manipulator(foo_time, 2, 02)
foo_time_manupulator(foo_time_2, 3, 02)
The problem with that is that the global variable doesn't change and so the sound effect loops continuously as every frame the function is re-read and it see that the global varaible remains at the value it was used as an argument, rather than the updated variable.
Sorry if this question has been poorly worded, it's a little difficult to articulate.
Is there any way in lua to be able to manipulate a global variable that is used as an argument?
A good qualifier may be that this code is being looped, and so requires an outside variable to act as a counter.
You can try this:
function foo_time_manipulator(global_var_name, len, six)
local global_var = _G[global_var_name]
global_var = global_var + 2
if global_var < len then
play(sfx)
end
end
and use it like this
foo_time_manipulator("foo_time", 2, 02)
foo_time_manupulator("foo_time_2", 3, 02)

How can I create a foreach loop with "Foreach From Variable Enumerator" in SSIS programmatically

I'm trying to create an SSIS package dynamically using DTS Runtime classes.
I'm trying to create a foreach loop using "Foreach From Variable Enumerator", where my variable contains an ArrayList with the necessary collection.
I'm unable to find a way through which i can set the variable (under enumerator configuration, as seen in VS) which will hold the collection for looping.
I'm not finding any options in forEachLoop.ForEachEnumerator. Nor am I able to cast forEachEnumeratorHost.InnerObject to an enumerator I want. I found on MSDN a class Microsoft.SqlServer.Dts.Runtime.Enumerators.FromVar.ForEachFromVarEnumerator. But I'm not able to find the FromVar class in Enumerators. Am i missing something grave?
Has anyone else done this? I find it difficult to believe that something which can be done so easily through Visual Studio UI, cannot be done programmatically.
Below is the code I have now...
ForEachLoop forEachLoop = p.Executables.Add("STOCK:FOREACHLOOP") as
ForEachLoop;
// Create a VariableMappings and VariableMapping objects.
ForEachVariableMapping forEachVariableMapping =
forEachLoop.VariableMappings.Add();
// Create a mapping between the variable and its value.
forEachVariableMapping.VariableName = #"User::CurrentTableName";
forEachVariableMapping.ValueIndex = 0;
ForEachEnumeratorInfo forEachEnumeratorInfo =
runtimeApp.ForEachEnumeratorInfos["Foreach From Variable Enumerator"];
ForEachEnumeratorHost forEachEnumeratorHost =
forEachEnumeratorInfo.CreateNew();
You are almost there...I think you are forgetting to set the ForEachEnumerator property of the forEachLoop object.
ForEachLoop forEachLoop = p.Executables.Add("STOCK:FOREACHLOOP") as ForEachLoop;
ForEachEnumeratorInfo forEachEnumeratorInfo = runtimeApp.ForEachEnumeratorInfos["Foreach From Variable Enumerator"];
ForEachEnumeratorHost forEachEnumeratorHost = forEachEnumeratorInfo.CreateNew();
//forEachEnumeratorHost.CollectionEnumerator = false; // true or false; set accordingly.
// cast the inner object to ForEachFromVarEnumerator
ForEachFromVarEnumerator = forEachEnumeratorHost.InnerObject as ForEachFromVarEnumerator;
// Now that you have the ForEachFromVarEnumerator, set its properties.
// For variable name, do not forget the variable's name space and the variable name (separated by ::)
ForEachFromVarEnumerator.VariableName = "var_namespace" + "::" + "var_name";
// finally.....
forEachLoop.ForEachEnumerator = forEachEnumeratorHost; // DO NOT FORGET THIS. Here you are setting what the actual enumerator would be
You can do like this:
Script task
Make sure MyList variable is Writeable
ArrayList NewList = new ArrayList();
NewList.Add("Ost");
NewList.Add("Hest");
Dts.Variables["User::MyList"].Value = NewList;
Dts.TaskResult = (int)ScriptResults.Success;
The first step is
Add an assembly reference to Microsoft.SqlServer.ForEachFromVarEnumerator.dll and then one can get access to the Microsoft.SqlServer.Dts.Runtime.Enumerators.FromVar.ForEachFromVarEnumerator class.
And then the rest of the answer is exactly what #Sam has mentioned above.

Assign a function to a variable recursively

I've read a lot about that argument here in stackoverflow. Anyway I can't really understand what really happens to a variable when I assign a recursive function to it ! I've tried so hard using printNow() command just to undestand what happening .. but nothing , just a bunch of None !
variable = function() #variable is going to be the the return value of function() ?
#if function() is a recursive function each time of the recursive, variable
#is gonna be different ?
edit :
Added a piece of code that i can't understand ..
def permute(seq):
if len(seq)<=1:
perm=[seq]
else:
perm=[]
for i in range(len(seq)):
sub=permute(seq[:i]+seq[i+1:]) # What sub is gonna be = ?
for p in sub:
perm.append(seq[i:i+1]+p)
return perm

Specify whever a lua parameter should be a copy or a reference

I'm wondering if there is a way to specify if the parameters of a lua function should be copied or just referenced. Color is an object representing a color.
For example, with this code
function editColor(col)
col.r = 0
print(tostring(col.r))
end
color = Color(255, 0, 0)
print(tostring(color.r))
editColor(color)
print(tostring(color.r))
Makes the output
255
0
0
So col is a "reference" to color, but this code:
function editColor(col)
col = Color(0, 0, 0)
print(tostring(col.r))
end
color = Color(255, 0, 0)
print(tostring(color.r))
editColor(color)
print(tostring(color.r))
Makes this output
255
0
255
So here the color is copied.
Is there a way to force a parameter to be copied or referenced? Just like the & operator in C++?
No, parameters in Lua are always passed by value (mirror). All variables are references however. In your second example in editColor you're overriding what the variable col refers to, but it's only for that scope. You'll need to change things around, maybe instead of passing in a variable to be reassigned, have the function return something and do your reassignment outside. Good luck.
This will do what you want. Put the variable that you want to pass by reference into a table. You can use a table to pass anything by ref, not just a string.
-- function that you want to pass the string
-- to byref.
local function next_level( w )
w.xml = w.xml .. '<next\>'
end
-- Some top level function that you want to use to accumulate text
function top_level()
local w = {xml = '<top>'} -- This creates a table with one entry called "xml".
-- You can call the entry whatever you'd like, just be
-- consistant in the other functions.
next_level(w)
w.xml = w.xml .. '</top>'
return w.xml
end
--output: <top><next\></top>
Lua is a bad language. Suffer its simplicity when you need to do something just a little complex.