Google application script <?= var ?> - google-apps-script

I am trying to print a variable from the .gs code within an HTML document using
<?= var ?>
I know I have done this before, but when I do it now, the entire tag is just rendered - so the page displays
<?= var ?>
rather than do anything with GAS.

Yea, that checks out. You CAN use the .createTemplateFromFile with viewport, and must use .createTemplateFromFile not .createTemplateFromFile with any scripting.
Simple enough, just don't use it often enough to keep it all straight.

Related

What is the difference between the Google App Script Scriptlets? [duplicate]

Is the tag <?= ?> a specific syntax of Google scripts or it could work in a pure html/javascript page? Is there any description for it?
That is similar to <?=$a; ?> PHP tag but I doubted when I saw this: <? var foo = "test"; ?>.
Officially, these bits of code are referred to as 'scriptlets' in GAS documentation. They are Apps Script syntax for server-side HTML rendering.
Before HTML content is sent to your browser for client-side rendering all scripts between <? ?> and <?! ?> are executed and their output gets appended to the template.
It could also be just plain text between these tags instead of scripts.
Scriptlets get executed when you convert an HtmlTemplate to a HtmlOutput object in GAS:
//HtmlTemplate instance
var htmlTemplate = HtmlService.createTemplate("<a href='<?!= www.google.com ?>'>Google</a>");
//HtmlOutputInstance - calling evaluate() fires off the scriptlets and
//creates HTML output that is ready to be sent to the client.
var htmlOutput = htmlTemplate.evaluate();
//Logs <a href='www.google.com'> Google </a>
Logger.log(htmlOutput.getContent());
The short answer is no. You can't run scriptlets directly in your browser - they are executed on Google Servers. You are correct that there are many templating engines that serve the same function but are implemented differently.

yii2 how to use response sendFile() with pjax

I have observed that when I execute the function:
Yii::$app->request->sendFile() within a row with a gridView, instead of launching the file, it shows it embedded in the HTML.
Then if I remove the Pjax::begin() and Pjax::end() borders that enclose the GridView, Then download works.
How can I work with both functionalities without losing one of them?
This was discussed at Yii2 the solution for now is to use this method:
<?php Pjax::begin([
'id' => 'list',
'linkSelector' => '#list a:not([data-pjax=0])'
]); ?>
custom js or simple link to your action with download
pjax link
<?php Pjax::end(); ?>
It looks like this feature may be included in future releases.

How to get page with ajax?

I wanna get the page which contains some php and some html code, like this:
Page url: ahax.php?some=1&any=2
Page content:
<?php
some php code
?>
some html
<?php
some php
?>
I always use "echo" in php file for ajax, now it contains more row of html and php code, it is foolish to use "echo".
<?
$url = 'http://www.google.com';
echo file_get_contents($url);
?>
Not sure that I understood what you want to know... however I hope this is what you are looking for. Please, clarify your question.
Also, have a look at this very helpful link.

Google Apps Script Web App: Can't separate out css and js code

I'm trying to separate my javascript and stylesheet from the main HTML file in my Google Apps Spreadsheet script that is published as a Web App. I have seen this answer to the problem, but I cannot get this approach to work for me.
When I have my stylesheet and javascript in the main HTML file, it works fine. When I try to separate them exactly as the answer recommends, the stylesheet and javascript code is not processed and instead the line that calls 'getContent()' is displayed in my browser. It looks like getContent() is not being executed at all.
I've tried moving my code away from the Spreadsheet to a Standalone Web App but still have the same problem. Any ideas why it's not working for me? Thank you!
A bit from my Code.gs:
function doGet() {
var output = HtmlService.createHtmlOutputFromFile('index');
output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
output.setTitle('Dashboard Tool');
return output;
}
function getContent(filename) {
Logger.log('getContent()');
return HtmlService.createTemplateFromFile(filename).getRawContent();
}
index.html:
<?!= getContent("stylesheet") ?>
<div class='header'>Dashboard</div>
<div id=content>
Content Here
</div>
<?!= getContent("javascript") ?>
'stylesheet.html' code is surrounded by the style tag and 'javascript.html' code is surrounded by the script tag.
You forgot evaluate() in the createHtmlOutputFromFile(), also you should use the createTemplateFromFile, as such.
var output = HtmlService.createTemplateFromFile('index').evaluate();
As #brian-p pointed out, you need the 'Template' instead of 'Output', for the evaluate occur on the scriplets <?!= ?>.
In this line of code:
return HtmlService.createTemplateFromFile(filename).getRawContent();
createTemplateFromFile() is being used. That method is for creating the original template, but the getContent() function is not for that purpose. Use:
return HtmlService.createHtmlOutputFromFile(filename).getContent();

Load new HTML page in Parent from iFrame Child

I have an HTML page with an <iframe> that loads an HTML form. The form inside the iFrame posts to a PHP script.
Upon completion of the PHP script, how can it load an entirely different HTML page outside of the iFrame?
not in php, php syntax is
header( 'Location: url' ) ;
but it only works if you havent done anything else yet.
but you can do it with javascript like so
document.location.replace("url")
or
document.location.href = "url";
so with php you can write (after form is submitted)
echo "<script>document.location.replace('url');</script>";
or
echo "<script>document.location.href = 'url';</script>"
edit
i changed window (redirects iframe) to document (redirects page) i should work now, sorry about that.
you can also use top.window.location i think.
a simpler way to do this is in the actual html <form action="url" target="_top">. however this will skip form-validation.