Conditional using site_url is always false - expressionengine

I am trying to use the following conditional statement in my template:
{if "{site_url}" == "http://dev.site.com" }
true
{if:else}
false
{/if}
When I test outputting site_url in the template I get http://dev.site.com, but this expression always evaluates false.
I've tried variations without brackets and quotes with no luck.

Try adding a custom variable to your config.php (/system/expressionengine/config/ folder):
//### Custom Variables ###
global $assign_to_config;
$protocol = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$assign_to_config['global_vars'] = array(
"root_url" => $protocol.$_SERVER['HTTP_HOST'],
"domain" => $_SERVER['HTTP_HOST']
);
Then change your template to be:
{if "{root_url}" == "http://dev.site.com" }
true
{if:else}
false
{/if}
or
{if "{domain}" == "dev.site.com" }

You might get a better answer in https://expressionengine.stackexchange.com/ but some ways to work around parse order issues include:
php on input
passing variables through embedded templates
low variables
writing your own plugin
exp:query calls

Related

Exclude Externals from jsDoc output

How can we exclude Externals section from jsDoc output correctly?
I have lots of externals in my project that I do not want to show on the navigation panel at all, as it takes up all the space, and is useless to us.
I have been able to hack it manually, by changing the code in file node_modules\jsdoc\lib\util\templateHelper.js, but this is not a reusable approach for my team of developers.
The hack was in overriding members.externals with an empty array:
/*
members.externals = members.externals.map(function(doclet) {
doclet.name = doclet.name.replace(/(^"|"$)/g, '');
return doclet;
});
*/
members.externals = [];
Unfortunately, after years of using jsDoc, all I can do is to continue re-hacking it after every update of the dependency, in file node_modules\jsdoc\lib\util\templateHelper.js.
Fortunately, even with the current version 3.5.5, the hack still works the same:
// HACK: set Externals to an empty list:
members.externals = []; /*members.externals.map(function(doclet) {
doclet.name = doclet.name.replace(/(^"|"$)/g, '');
return doclet;
});*/
I'm not sure this is much more elegant, but this is what I ended up doing. It works for all projects without having to hack the jsdoc install.
My problem: I have classes derived from imported node_modules and I wanted the inherited information included in my documentation, but I didn't want the base classes clogging up the sidebar navigation.
First, I added a plugin to my jsdoc-template (I called it skip-node_modules):
exports.handlers = {
processingComplete: function (e)
{
for (let i = 0; i < e.doclets.length; i++)
{
const doclet = e.doclets[i]
if (!doclet.undocumented && doclet.meta && doclet.meta.path.indexOf('node_modules') !== -1)
{
// hack the name so I can find it in the .tmpl file
// I tried to add a new flag to the doclet, but it didn't pass through
doclet.longname += '~'
}
}
}
};
Then I added a check in the navigation.tmpl:
<ul class="list">
<?js
this.nav.forEach(function (item) {
?>
<?js if (item.longname[item.longname.length - 1] !== '~') { ?>
<li class="item" data-name="<?js= item.longname ?>">
...
<? } ?>
And poof, no more node_module imports in my sidebar. (This does have the downside of not showing a sidebar when you click through to these classes. I'm sure with more hacking I can get rid of that, but it wasn't too important.)
It would be easy to change this to check for external symbols. console.log(e) in the plugin to get all the info the doclets provide and find what works for your situation.

rails 4 simple search form for users to find each other

I am trying to implement simple search form for users to look up other users.. I have been looking around the web for quite some time, but lot of the resources seem to be outdated, either for rails 3 or retired gems...
Can anyone pin point me to a recent rails 4 resources for simple search or show me the skeleton code to start? thank you in advance!
https://github.com/jhund/filterrific
scope :search_query, lambda { |query|
return nil if query.blank?
terms = query.to_s.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conds = 2
sql = "(LOWER(foo.first_name) LIKE ? OR LOWER(foo.last_name LIKE ?)"
where(
terms.map { |term| sql }.join(' AND '), *terms.map { |e| [e] * num_or_conds }.flatten
)
}
That would be a simple example of searching a user by either first_name or last_name.
Filterrific is quite good, but can be heavy on the back side when it does the query if you have many records.
For a very simple search on Users username you can do in your view :
<%= form_tag users_path, :method => 'get', :id => 'users_search' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
In your User model you must define a 'search' method :
#users_controller.rb
def self.search(user_name)
if user_name
user_name.downcase!
where('LOWER(name) LIKE ?', "%#{user_name}%")
else
all
end
end
and finally in your controller you can call this method :
def index
#users = User.search(params[:search])
end
The route can be defined as a GET like your default route for the page :
#routes.rb
resources :users

ExpressJS + EJS: Avoid "variable is not defined" globally

How to globally avoid EJS throw error when context variable is not defined ? I mean a way to set (in middleware?) for example space or null value for all variables not defined. Now i always use on template something like below. Is better way ?
<% if(typeof locals.variable != 'undefined') { %>
...
<% } %>
I usually just have a middleware that sets some default values for a particular set of routes via res.locals.variable = undefined; or similar. Then you can successfully check if (variable === undefined) in the template for those routes.

using default value with an ejs filter

If item.description is undefined or empty, I want to default to 'No description':
I've tried the following:
<%-: ( item.description | markdown ) || '<p>No description</p>' %>
<%-: ( item.description || 'No description' ) | markdown %>
What else can I do?
Not sure if you can mix || with the | of EJS' filters, but you can add a filter to accomplish it:
ejs.filters.or = function (arg, sub) {
return arg || sub;
};
<%-: item.description | or:'No description' | markdown %>
I'm wondering the code with your question, why the pre-tag with ejs template expression is "<%-" not the "<%=" (the output or value assignment output) or the "<%=:" (the filter output) signature?
BTW, the filter invoke signature is the single vertical bar not double, and you can invoke any times with what you want...

Is it possible to check if cookies are enabled with modernizr?

I was researching about how to check if the cookies are enabled in a browser and i found a lot of answer, i even tested a few ones, but after that a friend of mine suggest me to use Modernizr for that.
I started to search about that and i found a lot of stuff related with CSS3 and HTML5, but i don't want that, i just wanna know if is it possible to check that cookies are enabled or not with Modernizr?
check this url, hope it's helpful :
https://github.com/Modernizr/Modernizr/commit/33f00fbbeb12e92bf24711ea386e722cce6f60cc
Below code is copied from http://sveinbjorn.org/cookiecheck.
function are_cookies_enabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? true : false;
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
{
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
}
return (cookieEnabled);
}
A direct answer to the question is 'Yes!' and it is built in
Example code:
if (Modernizr.cookies == false) {
alert('Please enable cookies');
}
else {
// do something with cookies
}
You can also use the css class .cookies or .no-cookies to show/hide a panel telling the user they need cookies enabled.
.cookies #noCookies
{
display: none;
}
<div id='#noCookies'>
This site requires cookies! Please turn them on already!
</div>
(This .cookies class is added to <body> tag by Modernizr).
Note: If you are creating a custom build of Modernizr the cookies option is currently 'hidden' under the 'Non-core detects' section.
Another way with PHP
HTML/PHP:
<?php
session_start();
$_SESSION['cook'] = 1;
echo "<img src=\"cookcheck.php\">";
?>
PHP - cookcheck.php:
<?php
session_start();
if ($_SESSION['cook'] !== 1)
{ $image="/nocookmsg.png"; } # Cookies NOT Enabled
else { $image="/blank.png"; } # Cookies Enabled
$img=imageCreateFromPNG($image); # Create Image
header("Content-type: image/png"); # Send Header
imagePNG($image); # Send Image
?>

Resources