Jump to content


Photo

using existing classes


  • Please log in to reply
4 replies to this topic

#1 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 21 April 2011 - 07:36 PM

Hello peoplez of etomite,

I need to integrate an existing class in an etomatized site.
I dont even know where to begin trying this.
Is this possible at all/
How should I start?

cheerz,
v

#2 Ralph

Ralph

    Loves Etomite Forums!

  • Admin
  • 6,539 posts

Posted 21 April 2011 - 09:53 PM

More information would be helpful... What features does the new class provide...???

There are several ways to use additional classes within Etomite... The simplest way is to just use an include and then instantiate an iteration of the class in stand-alone mode... Another way would be to add "extends etomite" into the class name which "should" add the class functions to the Etomite object...

If you look at the bottom of the Etomite parser, index.php, you will see a line that includes my formClass class... To use it you would add the following lines into a snippet...

$f = new formClass;


#3 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 22 April 2011 - 08:48 AM

Thanks for your quick reply Ralph, I am puzzled by what you mean with :

The simplest way is to just use an include and then instantiate an iteration of the class in stand-alone mode...

Include in what? index.php?
What do you mean with 'stand-alone mode'?

Another way would be to add "extends etomite" into the class name which "should" add the class functions to the Etomite object...


This looks like a smooth way of doing it, however, if possible I would prefer not to touch the etomite core files.
I am afraid things will break if ever there should be a new update of etomite (wink-wink ;) )

#4 Ralph

Ralph

    Loves Etomite Forums!

  • Admin
  • 6,539 posts

Posted 22 April 2011 - 03:16 PM

Here is a simple example... The first block of code is the stand-alone class file which can be called from within a snippet for this example which is the most common method of using an external stand-alone class... The second code block is the snippet that includes and uses the class... By placing the snippet call [!test_class!] in a document or in a page template you will see the resulting code rendered by the snippet and class...

<?php
// test_class.php

class test_class
{
  public $testvar1;

  function __construct()
  {
	$this->testvar1 = "<p>Class Instantiated</p>";
  }

  function func1($text)
  {
	return "<p><b>{$text}</b></p>";
  }
}

?>

// Snippet:: test_class
$classPath = "relative_path_to_the_class"; // Example: "./manager/includes/"
include($classPath . "test_class.php");
$t = new test_class;
$output .= $t->testvar1;
$output .= $t->func1("This is a sample bold paragraph of text.");
return $output;

Now, this is a very simple example... If you want to use the class globally within Etomite then you can either add the include into the parser, as stated earlier, or you can use a separate snippet call within the <head> section of your page template to do a single include and then you could use the class from within multiple snippets or with repeated calls to the same snippet... To do that you might put something like the following in the snippet called from the <head>...

// include_test_class snippet
$classPath = "./manager/includes/";
include($classPath . "test_class.php");
$t = new test_class; // optionally instantiate the class at load time

From there you can use the class anywhere within Etomite by referencing the $t-> class object just as you reference the $etomite-> class object... To do this you would add the [!include_test_class!] snippet call in the page templates <head> section and then omit the include related code from any snippets making calls to the class...

Hope this helps...

#5 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 27 April 2011 - 08:33 AM

thanks ralph, good tutorial, will definitely look into this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users