Trying to insert a variable into a string while adding the string itself to an existing array like this:
var myVar = 'slug';
myArray.push(['item one', '/path/to/' + myVar + '/']);
This approach is not yielding the expected results. Despite correctly assigning a value to myVar
, the array does not recognize it as a variable.
Seems simple, but obviously missing a key detail here.
Appreciate any assistance on this matter.
UPDATE: Issue not related to Javascript.
As Alex pointed out, this typically should work. (And yes, the array is already existing.)
However, I failed to mention that the push method is embedded within Django templating logic to only execute on my development instance, as shown below:
{% ifequal INSTANCE 'DEVELOPMENT' %]
myArray.push(['item one', '/path/to/' + myVar + '/']);
{% endifequal %}
I didn't think the logic was causing problems. I confirmed that:
- the logic was functioning correctly
- INSTANCE was indeed 'DEVELOPMENT'
- and the JavaScript line was present in the source code, indicating no issues with the logic.
However, removing the Django logic allows the JavaScript to work as intended.
Curiously, this server-side logic is preventing any external values from being inserted into the array, whether client- or server-side.
In an attempt to troubleshoot, I tried this...
myArray.push(['item one', '/path/to/{{ block slug }}undefined{{ endblock }}/']);
...with the following code in a child template.
{{ block slug }}slug{{ endblock }}
The issue persists - when the template logic is active, the value becomes /path/to/undefined
. Without it, the value appears as /path/to/slug
as expected.
The cause of this anomaly remains unclear to me. While I can find a workaround, open to any insights anyone might have.
Thanks once more.
Newcomer question: what's the protocol for changing post titles on Stack Overflow?