Can we access twig inner property on construction? - twig

Let's say I'm constructing a Twig object and I want to access an inner property defined previously:
{% set my_object =
default_width: 12,
default_height: default_width * 2
%}
This throws a Variable "default_width" does not exist error, which make sense since the object was not entirely declared yet but is there a way to make this work without two instructions?

This behavior isn't possible, the best way to do it is to declare a variable before the construction and reusing it here.

Related

is there a better way of initializing correctly scoped variables inside if statements

I'm curious to know if there is there a prettier way of correctly scoping objects that are initialised inside of if statements
say you have the following script:
if (x){
create object_A
}
print(object_A)
the above will fail because object A is out of scope for the print so the obvious thing to do is initialise a null variable before the if statement like this:
variable_to_reference_object_A = null
if (x){
variable_to_reference_object_A = create object_A
}
print(variable_to_reference_object_A)
This just always makes me feel like a silly billy because I'm basically saying is:
variable_to_reference_object_A = i haven't a clue because i haven't created it yet
very curious to see what you guys have to say about this, as id love to know if I'm completely doing things wrong (and possibly why it seems to show up in almost all languages). TIA

What's the difference in TWIG between using the attribute() function and using brackets

I'm trying to understand twigs attribute() function, because it seems to me that this
attribute(object, parameter)
and this
object[ parameter ]
Would do the same thing. They return the value of that parameter in the object. I realize that brackets can also be used to get a value from an array with an index, but if the parameter is a string it seems to get the value from the parameter just as well. What else does attribute() do that Im missing?
The attribute function can also be used to call methods of objects and enables you to pass arguments towards it
{{ attribute(object, method, arguments) }}

duktape, modify variable argument in native C code

I'm trying to modify a variable passed in an argument in a native function like this:
var MyVar = 'foo';
myNativeFunc(MyVar);
and inside my native, I can read the content of MyVar, with :
std::string(duk_to_string(ctx, 0));
but, I need to modify the value of this variable inside native function.
what is the best way for does it? (I can't use the return statement) thanks.
That's not possible as it would implement pass-by-reference (which is not supported in JS). Read also the answer to the question Pass Variables by Reference in Javascript
So, either pass in an object reference and change the object properties or use a return value.

Equality operator in Jade

I am trying to write a mixin in Jade. I would like to pass a parameter through mixin to build same type of block with different value at different states. In the mixin I want to write a conditional that checks if passed parameter is equal to some value. But unfortunately it is not working and I can't find proper documentation anywhere. My code section is following:
mixin test(id)
if territoryList
each val in territoryList
- if (val.parentArea==id){
button.btn.btn-primary.btn-block=val.name
- }
And I am calling it like test('1')
What is the problem?
I don't think your if is supposed to be a javascript if.
Don't you want this ?
mixin test(id)
if territoryList
each val in territoryList
if val.parentArea==id
button.btn.btn-primary.btn-block= val.name

Is it possible to take the name of a variable and turn it into a string in ActionScript 3.0?

I am making a simple debugger window in ActionScript for myself where I can add and remove variables I want to track. I was to be able to add variables to the list by just doing something like
DebuggerMonitor.trackVar(variable).
My question is, is there any way I can turn "variable" itself (the name, not the value) into a String to be added into a text field?
Depending on how "intelligent" your debugger should be, you could just pass the name along:
DebuggerMonitor.trackVar( variable, "variable" );
since obviously, when used in a context like this, the name should be known at the time you are writing the program.
You can also do some reflection magic to get instance variable names, but it won't work for temp variables (their names are dropped at compilation time):
public function getVariableName( instance:*, match:* ):String {
var typeDescription:XML = describeType( instance );
var variables:XMLList = typeDescription..variable;
var accessors:XMLList = typeDescription..accessor;
for each(var variable:XML in variables)
if(matchesXMLName( instance, variable, match ))
return variable.#name;
for each(var accessor:XML in accessors)
if(matchesXMLName( instance, accessor, match ))
return accessor.#name;
return "No name found.";
}
private function matchesXMLName( instance:*, xml:XML, match:* ):Boolean {
return match == instance[xml.#name.toString()];
}
var varName:String = getVariableName ( myObject, variable );
Using reflections like this will also be quite costly, if used often - you will have to think of a way to cache the type descriptions.
I recommend you check out the as3commons reflections package - there is a lot of useful functionality in there...
Short answer - No :(
You can access the type name but not individual instance names, as these are lost at run-time.
There is a confusion caused by the keyword 'var' because it is used to create several types of bindings.
Lexical bindings (the keyword 'var' was used inside a function).
Dynamic bindings (the keyword 'var' was used to declare a class' field).
Lexical bindings are interpreted by the compiler at compile time as addresses of the registers of the registers space occupied by the function. The names given to lexical bindings perish at this time and it is not possible to restore them at runtime - therefore you can't get the "name" of the variable.
Dynamic bindings are a kind of "public API" of the objects that declare them, they may be accessed from the code that was not compiled together with the code that created them, this is why, for the purpose of reflection the names of these bindings are stored in compiled code. However, ActionScript has no way of referencing LHS values, so you cannot, even if you know the name of the variable and the object declaring it, pass it to another function. But you can look it up in the debugger or by calling describeType on the object declaring the variable. Note that describeType will not show information on private variables even if you are calling it from the scope of the object in question.

Resources