Sorry of this has been asked before, but I can't find it.
Why does the following fail with
Uncaught --> Smarty Compiler: Syntax error in template
"/home/ezms/public_html_subs/dev4/design/backend/templates/index.tpl"
on line 31 "{""|foo }" unknown modifier "foo"
{if false} {""|foo} {/if}
I have certain code conditionally included I.e.
{if $x_enabled} {
function foo() {
return "hello";
}
}
I would expect that if the smarty IF condition did not evaluate to true that the modifier would not be executed. Where am I going wrong?
Smarty is a compiler and uses a lexer to convert its code syntax into PHP. Then Smarty has PHP execute the code.
When compiling, it will fault on the unrecognized modifier. If the modifier was recognized, the lexer would convert that to a real function (based on what the plugin does) -- for example, foo(""). Being a modifier plugin, you are free to make the plugin do much more work deemed necessary, even if the result of any action on the argument is not used.
Then...
The compiled PHP code would be something like:
if(false){$smarty_user_functions->foo("");}
If the compile is successful, Smarty then has PHP execute that code, where PHP employs its own optimizing algorithms converting the human-readable script to token/opcode/bytecode. If when PHP runs this code, it will decide what to do on a false condition.
You should add your foo modifier function in smarty plugin directory (https://www.smarty.net/docs/en/plugins.modifiers.tpl)
file would have a name modifier.foo.php
and contain code:
function smarty_modifier_foo()
{
return 'hello';
}
then, your code {if false} {""|foo} {/if} in template will works.
Related
The following example of Kotlin source code returns an error when compiled:
fun main() {
var index: Int // create an integer used to call an index of an array
val myArray = Array(5) {i -> i + 1} // create an array to call from
val condition = true // makes an if statement run true later
if (condition) {
index = 2 // sets index to 2
}
println( myArray[index] ) // should print 2; errors
}
The error says that the example did not initialize the variable index by the time it is called, even though it is guaranteed to initialize within the if statement. I understand that this problem is easily solved by initializing index to anything before the if statement, but why does the compiler not initialize it? I also understand that Kotlin is still in beta; is this a bug, or is it intentional? Finally, I am using Replit as an online IDE; is there a chance that the compiler on the website simply is an outdated compiler?
The compiler checks whether there is a path in your code that the index may not be initialized based on all the path available in your code apart from the value of the parameters. You have an if statement without any else. If you add the else statement you will not get any compile error.
I am able to write a lint warning/error with a QuickFix to change some code in the IDE.
I also know that I can tag a Kotlin function with the #Deprecated tag, like so:
#Deprecated(
level = DeprecationLevel.ERROR,
message = "This is wrong",
replaceWith = ReplaceWith("doRight(input)")
)
fun doWrong(input: String) {}
This generates a lint quickfix (LintFix) to replace the function. But it also generates a lint quickfix to replace the function in your entire project, like so:
How do I write a custom LintFix that applies the quickfix not just to the current warning, but to all applicable warnings in the project?
I would like to use a variable within a Less mixin that when passed to :extend has the same result as if I had instead used :extend with a class name string.
In the example below I have commented out a line that produces the CSS output I want by using :extend with a string.
But how can I do this with the #class-name variable instead?
.class-to-be-extended {display: block;}
.my-mixin (#class-name) {
#class-string: ~".#{class-name}";
.my-extra-class {
// &:extend(.class-to-be-extended); // works
&:extend(#{class-string}); // doesn't work, but no errors
}
}
.my-mixin(class-to-be-extended);
The CSS output I would like is:
.class-to-be-extended,
.my-extra-class {
display: block;
}
At the time of writing this I'm using the latest version I can find which is Less 1.4.2
This is not possible with Less version 1.4.2. (Thanks to Jon Schlinkert for letting me know via Twitter).
A feature request has been submitted to https://github.com/less/less.js/issues
I don't know whether all coffeescript compilers wrap their scripts in anonymous functions, but that's what I see Rails doing. How can I disable this encapsulation?
I want to put several initializing functions in a single coffeescript file, then call one of them from an on-page <script> tag (so that each page calls a different initializer). This can't be if the initializing functions are encapsulated.
Coffeescript initializer functions:
initializerA = -> console.log 'foo'
initializerB = -> console.log 'bar'
On-page code:
<script>$(document).ready(initializerA)</script>
Sys: coffee-rails 3.2.1, Rails 3.2.3, Ruby 1.9.3
Coffeescript documentation says that all script will be wrapped in an anonymous function for the sake of encapsulation/safety. To make something accessible within the global scope do the following:
window.myvar = myvar
You can put several into a single file by doing something like this:
((Demo, $, undefined_) ->
Demo.utils = Demo.utils or {}
Demo.utils.bacon = (->
alert("bacon called")
)()
Demo.utils.eggs = (->
alert("eggs called")
)()
) window.Demo = window.Demo or {}, jQuery
Then in your page just call the appropriate method:
Demo.utils.bacon();
A good explanation of this pattern can be found here.
I'm doing an experiment with wxWebConnect test application, incorporating the xpcom tutorial at "http://nerdlife.net/building-a-c-xpcom-component-in-windows/"
I adapt MyComponent class as necessary to compile together with testapp.exe (not as separate dll), and on MyApp::OnInit I have the following lines:
ns_smartptr<nsIComponentRegistrar> comp_reg;
res = NS_GetComponentRegistrar(&comp_reg.p);
if (NS_FAILED(res))
return false;
ns_smartptr<nsIFactory> prompt_factory;
CreateMyComponentFactory(&prompt_factory.p);
nsCID prompt_cid = MYCOMPONENT_CID;
res = comp_reg->RegisterFactory(prompt_cid,
"MyComponent",
"#mozilla.org/mycomp;1",
prompt_factory);
Those lines are copied from GeckoEngine::Init(), using the same mechanism to register PromptService, etc. The code compiles well and testapp.exe is running as expected.
I put javascript test as below :
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const cid = "#mozilla.org/mycomp;1";
obj = Components.classes[cid].createInstance();
alert(typeof obj);
// bind the instance we just created to our interface
alert(Components.interfaces.nsIMyComponent);
obj = obj.QueryInterface(Components.interfaces.nsIMyComponent);
} catch (err) {
alert(err);
return;
}
and get the following exception:
Could not convert JavaScript argument arg 0 [nsISupport.QueryInterface]
The first alert says "object", so the line
Components.classes[cid].createInstance()
is returning the created instance.
The second alert says "undefined", so the interface nsIMyComponent is not recognized by XULRunner.
How to dynamically registering nsIMyComponent interface in wxWebConnect environment ?
Thx
I'm not sure what is happening here. The first thing I would check is that your component is scriptable (I assume it is, since the demo you copy from is). The next thing I would check is whether you can instantiate other, standard XULRunner components and get their interface (try something like "alert('Components.interfaces.nsIFile');" - at least in my version of wxWebConnect this shows an alert box with string "nsIFile".
Also, I think it would be worth checking the Error Console to make sure there are no errors or warnings reported. A magic string to do that (in Javascript) is:
window.open('chrome://global/content/console.xul', '', 'chrome,dialog=no,toolbar,resizable');