cancel
Showing results for 
Search instead for 
Did you mean: 
j_denholm-price
Surveyor

Template variables or template code/language?

Is there a Canvas equivalent of Blackboard's "Template Variables"? My university is moving from Bb to Canvas and it's something I rely upon to pass information through an <iframe> to a web app. In PHP terms I guess these might also be considered a templating language and I can't find anything in these resources but I'm new here so apologies if it's asking the obvious!

Thanks -- James.

8 Replies
kenneth_rogers
Surveyor

 @j_denholm-price  - I went ahead and shared this with Canvas Admins‌ and Canvas Developers‌ for you. Hopefully it will help get a few more eyes on this thread for you.

Good luck, sir.

mcowen
Instructure
Instructure

 @j_denholm-price ‌ check out the ENV variable on any Canvas page. It is JSON and has some great info on both the user and their current location within Canvas.

I hope this helps!

jperkins
Instructure
Instructure

John, if you are looking at running something in an iFrame, I'd recommend that you use an LTI tool (and the available variables) for building out your content. 

Edu App Center - Basics - POST Parameters 

LTI Variable Substitutions - Canvas LMS REST API Documentation 

Thanks -- if the use case survives our move from Bb to Canvas I'll do that! The docs look comprehensible and it seems to do all that we currently need 🙂

Thanks -- I'd prefer a server-side solution like the Bb variables but could easily work with JSON. ENV contains some good stuff (thanks for the tip) but not, it seems (on our current Canvas anyway), the course name or University course (module) ID, or the native University user's ID, but instead uses whatever IDs I guess we've setup Canvas to use. Some chat with our IT support needed 😉

kenneth_rogers
Surveyor

Hi there,  @j_denholm-price !

I wanted to follow up on your question and see how things ended up for you. It looks like both Mike and Jeremy gave you some great feedback. How'd those work for you?

For the time being, and based on the conversation in this question, I am going to mark this question as "Assumed Answered." This won't prevent any further dialogue, it's just a way to keep the Community we all love a little more organized. We'd love to hear back from you and find out how you're doing with Canvas!

When I look at the Variable Substitutions page, I don't actually see any listed. Am I missing something?

dtod
Adventurer

Here's something I'm trying in the global javascript, and it seems to work.

// search and replace
function replaceInText(element, pattern, replacement) {
//console.log(pattern);
//console.log(replacement);
for (let node of element.childNodes) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
attr='href';
if (node.hasAttribute(attr)){
//console.log(node);
node.setAttribute(attr, node.getAttribute(attr).replace(pattern, replacement));
}
replaceInText(node, pattern, replacement);
break;
case Node.TEXT_NODE:
node.textContent = node.textContent.replace(pattern, replacement);
break;
case Node.DOCUMENT_NODE:
replaceInText(node, pattern, replacement);
}
}
}

function replaceLocalVariables(){
var user_content=document.getElementsByClassName('user_content');

Array.prototype.forEach.call(user_content, function(el) {
// define ENV fields here that you want to process
var fieldsToWork = ['ENV.sis_course_id','ENV.current_user.display_name'];
for (var i=0; i<fieldsToWork.length; i++){
var searchFor = fieldsToWork[i];
var re = new RegExp(searchFor, "g");
replaceInText(el, re, eval(fieldsToWork[i]));
}
});
}

$(document).ready(function () {

if (/^\/courses\/[0-9]+/.test(window.location.pathname)){
   ENV.sis_course_id=$('#breadcrumbs a')[1].innerText;
   replaceLocalVariables();
}

});