Bambang pramusintha

Author Archive

Knowing Hapax

In Hapax, JavaScript on September 17, 2012 at 2:43 am

Hapax is a simple but powerful text templating library for Java.

Hapax is suitable for constructing text output from Java code. The syntax is similar to Google’s ctemplate library, and emphasizes the separation of logic from presentation.

Hapax was designed to be easy to use and have minimal dependencies. Hapax does not depend on any existing web framework, and is suitable for use in servlets, scripting languages (Scala, Groovy, etc), and server-side applications.

Example

package com.xfltr.hapax.examples;
import com.xfltr.hapax.Template;
import com.xfltr.hapax.TemplateDictionary;
import com.xfltr.hapax.TemplateException;
class HelloWorldExample {
  public static void main(String[] args) throws TemplateException {
    Template tmpl = Template.parse("Hello, {{WORLD:h}}");
    TemplateDictionary dict = TemplateDictionary.create();
    dict.put("WORLD", "Iapetus");
    System.out.println(tmpl.renderToString(dict));
  }
}

Features

  • Hapax enforces a strict separation of “view” from application logic.
  • Hapax has zero dependencies on third-party code (except for JUnit).
  • Hapax is well unit-tested.
  • Hapax is based loosely on the syntax and behavior of Google’s ctemplate library.
  • Hapax generate any kind of textual output: config files, HTML, XML, CSS, etc.
  • Hapax templates do not require a compilation step. Templates are parsed at runtime.
  • Hapax has native support for some types of content escaping (JavaScript, XML, HTML, URLs).
  • Hapax does not expect your templates to come from disk: templates can be be generated at runtime, stored in a database, or any other location.
Advertisements

Knowing ctemplate

In C++, ctemplate, Tutorial on September 16, 2012 at 12:35 pm

CTemplate is a simple but powerful template language for C++. It emphasizes separating logic from presentation: it is impossible to embed application logic in this template language.

Here’s an example of how to use it: not complete, but gives a good feel for what the ctemplate API looks like.

Here is a simple template file:

Hello {{NAME}},
You have just won ${{VALUE}}!
{{#IN_CA}}Well, ${{TAXED_VALUE}}, after taxes.{{/IN_CA}}

Here is a C++ program to fill in the template, which i assume is stored in the file ‘example.tpl’:

#include <cstdlib>
#include <iostream>  
#include <string>
#include <ctemplate/template.h>  

int main() {
  ctemplate::TemplateDictionary dict("example");
  int winnings = rand() % 100000;
  dict["NAME"] = "John Smith";
  dict["VALUE"] = winnings;
  dict.SetFormattedValue("TAXED_VALUE", "%.2f", winnings * 0.83);
  // For now, assume everyone lives in CA.
  // (Try running the program with a 0 here instead!)
  if (1) {
    dict.ShowSection("IN_CA");
  }
  std::string output;
  ctemplate::ExpandTemplate("example.tpl", ctemplate::DO_NOT_STRIP, &dict, &output);
  std::cout << output;
  return 0;
}

If you are interested in this templating language but are programming in Java, consider Hapax, which is similar to ctemplate.

Knowing JSON Template

In JavaScript, JSON, Python, Themplate on September 16, 2012 at 12:26 pm

JSON Template is a minimal but powerful templating language, currently implemented in both Python and JavaScript.

To use it, simply copy the Python file or the JavaScript file into your project. Neither has any external dependencies.

Since there are over one bajillion template languages for Python alone, it deserves some explanation. But first here are some examples.

Simple example

>>> import jsontemplate
>>> jsontemplate.expand('Hello {name}', {'name': 'world'})
'Hello world'

This Python example should look familiar to many people.* The JavaScript version is a straightforward translation of this API.

Full Example

To read this example, keep in mind the following:

  • A variable can be substituted by surrounding its name with curly braces: {}
  • Template “directives” start with a period: .
  • In place of for loops and if statements, the basic idea is that the template declares sections and repeated sections. The sections are named after keys in the JSON data dictionary. The presence or absence of the keys in the JSON dictionary implicitly determines if and how many times a section is expanded.
  • If a JSON dictionary key is missing or empty ([], {}, or null), then the {.or} section is expanded.

These few constructs are surprisingly powerful.

The following example uses most of the language, but not all. Read the rest of this entry »

How To Specifying Elements in a Hierarchy

In Hierarchy, JQuery, Plugin, Website Tutorial, Wordpress Tutorial on July 27, 2012 at 9:40 am

As the before topic says that jQuery is great at letting us select a set of elements. But what if you want to select one set in a page, but not another?
Say you have some

ele

ments in a

element that you want to select, but you don’t want to select any

<p> elements outside the <div> element.

One way of selecting the <p> elements we want is by specifying a selector hierarchy. For example, we can ask jQuery to select only those <p> elements that appear inside a <div> element, which itself appears in a <body> element, like this:

$(‘body div p’)

This tutorial will help us to make the selection that we want, and it will also let us change the style class of the selected elements when the user clicks a button, using the following code:

$(‘body divp’).toggleClass(‘striped’);

Steps to Select Elements in a Hierarchy are :

enter the code to add the jQuery library and add four <p> elements in a <div> element and one outside the <div> element, so total five <p> elements.

 

<html>
<head>

<title>Selecting elements in a hierarchy</title>

<script type=”text/javascript”src=”jquery-1.5.1.js”>
</script>

</head>
<body>

<h1>Selecting elements in a hierarchy</h1>

<div>
<p>This is paragraph 1.</p>

<p>This is paragraph 2.</p>

<p>This is paragraph 3.</p>

<p>This is paragraph 4.</p>

</div>

<p>This is paragraph 5.</p>

</body>
</html> Read the rest of this entry »

How to Creating New HTML Elements

In HTML Element, JQuery, Srcipt PHP, Tutorial, Website Tutorial, Wordpress Tutorial on July 27, 2012 at 9:13 am

jQuery help us to create an HTML elements and insert them into a page.

If we pass a text string that spells out a new HTML element to the jquery ( ) or $( ) function (which are the same thing), jQuery will create that new element:

$(“<p>I’m a new &lt;p&gt;element!</p>”)

The upper code will display ”  I’m a new <p> element ! “

To actually get the new element into a page, we have to use a function like insert After () , passing a selector indicating the element that you want to insert the new element after.

This tutorial creates new <p> elements and inserts them into a page following a <p> element with the ID “first”, like this:

$(“

I’m a new <p>element!

”).insertAfter(“#first”);

To create new elements:

1. We are going to use text editor (such as notepad or Wordpad) to create our HTML page and give the file name as “create.html”.

2. Enter the code to add the jQuery libraryand add a element with the ID “first”. Read the rest of this entry »

In Uncategorized on July 20, 2012 at 1:02 am

How Method Callbacks are handled in WordPress 2.3+

Here is best explaination where you can building for a new private function was introduced to manage object referenced method callbacks.
The form of which is below.

$myplugin = new MyPlugin();

add_action( ‘init’, array(&$myplugin, ‘_init’) );

As you can see, the callback references the $myplugin variable along with the method in the second element. The reason this can cause a problem, is if you unset the variable afterwards.

$myplugin = new MyPlugin();

add_action( ‘init’, array(&$myplugin, ‘_init’) );

unset($myplugin);

If you think this will magically remove the reference in the hook array, you are mistaken. PHP will retain the reference to the object and will call the method. This will hinder, but not prevent the removal of the object’s callbacks later.

For a workaround, you can recreate the object reference and it should (purely conjecture) recreate the needed hook string to reference to previously added object reference callback. This will not work if two or more object references of the same class were created, so it is assumed that in most cases only one object reference will be created. If only one were created and destroyed, it might be possible to remove the callbacks from the action or filter using the Plugin API. To more explaination, lets see the original pages…

Function Documentation

Prior to WordPress 2.3.0, there was a bug that prevented object referenced method callbacks from being removed. In WordPress 2.3.x and upcoming WordPress 2.5, in order to remove the original method in the class, you have to use the original object reference. In PHP4, where objects are passed by value, this can be a problem, but PHP5 users shouldn’t have much to worry about as long as the variable they are using holds the original class reference.

Explanation of Problem Prior to WordPress 2.3

To demonstrate the problem, take a look at this code. If you were to run this prior to WordPress 2.3, the hook would not be removed because you changed the class property $test.

class MyPlugin
{
    var $test;

    function MyPlugin() {
        $this->test = false;
        add_action('init', array(&$this, '_init'));
        $this->test = true;
    }

    function init() {
        remove_action('init', array(&$this, '_init));
    }
}

This is a poor example, in that…

View original post 424 more words

How to Create a WordPress Plugin

In Plugin, Tutorial, Wordpress Tutorial on July 12, 2012 at 8:52 am

Wordpress plug in logo How to Create a WordPress Plugin so it can be Included in a Theme.

General approach to development is to create discreet features that can interact with each other in elegant ways. By keeping each set of features small, they are simpler by design and implementation – this makes both them less prone to bugs and easier to reuse. You can see this approach in action in many of Crowd Favorite’s WordPress products. For example you can see this sample created Admin Post Formats UI as a library that can be easily reused rather than coding that feature into our FavePersonal theme directly.Another example, Carrington Build includes a plugin to enables translation of it’s data when being pushed from staging to production by RAMP.

Here are some specific things to consider:

  1. Don’t use activation or deactivation hooks. There are actually lots of reasons why these are a bad idea, including multi-site considerations, upgrade considerations, etc. Instead, pick an opportune time to do a lightweight check to see if your plugin set-up needs to be run (perhaps check the existence of one of your settings – perhaps an “installed version” setting) and do the work you need to do at that time. Remember, you want to do these checks in admin hooks rather than front-end hooks – you don’t want to add unnecessary overhead to every public page load and you want to avoid race conditions.2
  2. Don’t reply on your code running prior to all code being loaded. Hook in at wp_loaded (previously this was done at init) to do any set-up work, etc. The only situation where this might not be possible is if you need to override a pluggable function, but the types of plugins that need to do this are (in my experience) less likely to be the type of plugin included in a theme.
  3. Don’t rely on settings existing in the database. This ties in with the first consideration as I often see default settings written to the database as part of the activation process. I take the approach of creating an accessor function in my plugin that holds a set of defaults for settings. Here’s some example code: Read the rest of this entry »

How To Add Geo Meta tag

In Joomla CMS, Meta Element, Meta Geo Tag on July 11, 2012 at 9:26 am

Historically, meta tag for the language and the country has been less reliable than conclude the language or country directly. For example, many webmasters also just copy / paste from the template of a friend without checking the meta tag values.

Unreliability of meta tags is why Google tends to not use them or give them less weight.

So webmasters here three points for considering to put the geo meta tag to your website/blog :

[1] Bing currently using geo-meta tag
[2] Google ignores meta tags geo
[3] Be careful when you copy or use an existing template template

Cortesy forum discussion at Bing Community.

” I suggest adding the following meta tag to your home page header and verify that there are no tags pointing to “en-gb”–especially if you are using the Joomla CMS “, Brett said specifically, with Joomla.

Brett Young from the Bing webmaster support team suggested to the webmaster to change the meta tag to:

<meta http-equiv=”content-language” content=”en-us”>

“We generally ignore geo-meta tags like that because we’ve found that they are generally incorrect [copy & pasted from a template, etc]”, Brett Young Said.

A Google Webmaster Help thread once again confirms that Google ignores the geo-meta tags. Those tags somewhat look like this and use to serve the purpose of telling search engines where the site is based:

<meta name=”geo.placename” content=”United States” />
<meta name=”geo.position” content=”x;x” />
<meta name=”geo.region” content=”usa” />
<meta name=”ICBM” content=”x,x” />

It seems like this is a common issue webmasters run into when using Joomla.

How to Add a Character Counter to the WordPress Post Excerpt Box

In Excerpt Box, JQuery, Tutorial, Wordpress Tutorial on July 9, 2012 at 10:11 am

Why would you need a character counter on your excerpt box?

Certainly it seems like a character counter in WordPress post token is far more useful for the post content meta box. However, the active theme you may have very specific requirements for how long should your quote. If you want to be a perfectionist about your quote and do not want to see them cut the ellipse, then you will want to keep the length of your quotes to a certain number of characters. This is where the quote character counter will help.

Here is a handy little trick of wpsnipp.com recently. This piece counter lets you add character to the quote box on the screen edit your posts Read the rest of this entry »

%d bloggers like this: