Перейти к содержанию
  • Create apps for IPS #2 — Customizations


    MrHaim

    Welcome to the next tutorial on how to create an application for IPS. In this article we will focus on implement CSS and JavaScript code into our page with resources.

    Go back to previews lessons

    If you don’t have code from previous tutorial you can download “Source code” form GitHub.

    When you download the files then you have to upload folder from GitHub to `applications` folder and install application in AdminCP => System => SITE FEATURES => Applications from “Applications not currently installed” section.

    1_l8JKEUY06xyF70btXkIodQ.webp.fe493ab989e558969c1793d29172ace3.webp

    Create CSS file

    Now our page is dark without any colors. Lets add them! We must create CSS file in our app. Go to files your application and open `dev/css` folder.

    We have 3 primary folders here (exactly like HTML from previous tutorial):

    • front — Display in front,
    • admin — Display in AdminCP,
    • global — Display in front and in AdminCP

    We can create style for page, `front` folder will be the best for us. Inside “front” folder create simple CSS file. I name it “pagetest.css”.

    1_IVOxE5qbqJwtuIjVboODyg.webp.0f79f969bc41e757079da8c31334f299.webp

    Let’s make change for our title on page. Create class in CSS and style it in out `pagetest.css`!

    .title {
      font-size: 30px;
      font-weight: bold;
      color: red;
    }

    Import CSS file into controller

    In next step we have to import this file into our controller which is responsible for displaying the page. Go to modules/front/helloworld/pagetest.php. In execute method import our CSS file like this:

    \IPS\Output::i()->cssFiles = array_merge(\IPS\Output::i()->cssFiles, \IPS\Theme::i()->css(fileName, appName, module)); 
    • filename — your `css` file,
    • appName — your appName (app name you can find in each php file by namespace for example `namespace IPS\helloworld`. “helloworld” this is my app name),
    • module — front/admin/global folder

    Full code here for me:

    /**
      * Execute
      *
      * @return void
      */
     public function execute()
     {
      \IPS\Output::i()->cssFiles = array_merge(\IPS\Output::i()->cssFiles, \IPS\Theme::i()->css('pagetest.css', 'helloworld', 'front'));
      parent::execute();
     }

    Edit HTML file

    Now we can add class into our title (h1). Go to edit HTML page in your files app dev/html/front/pagetest/index.html. Code for me:

    <ips:template parameters="" />
    <h1 class="title">{lang="hello_world_test_page"}</h1>

    Now we can check the result our work.

    http://localhost/index.php?app=helloworld&module=helloworld&controller=pagetes

    1_SjOL7DfNk6sAsnDXi-pxYA.webp.103ccdca9a872d9cadaf6de081fca80a.webp

    Yey! We have red title on our page!

    I use IPS CSS framework on page. Full code here for me:

    <ips:template parameters="" />
    <h1 class="ipsType_pageTitle ipsFlex-flex:11 ipsType_break ipsMargin_bottom title">
      {lang="hello_world_test_page"}
    </h1>
    
    <div class='ipsBox ipsPadding'>
      <div class='ipsMessage ipsMessage_error'>
        <span class='ipsMessage_code'>12345</span>
        This is an error message.
      </div>
    
      Created by aXenDev!
    </div>

    Result:

    1_d1qHjabIiD2D_thMksmY8g.webp.f06acab3556945ac51ffc2b0b11ca3c4.webp
     

    Friendly URL Structure

    Users doesn’t like URLs with queries and parameters. Now we change it by using Friendly URL Structure.

    You have to open `data` folder in your app and create `furl.json` file.

    Inside file you can define routing in your app. Show the example:

    {
      "topLevel": "helloworld",
      "pages": {
        "helloworld": {
          "friendly": "",
          "real": "app=helloworld&module=helloworld&controller=pagetest"
        }
      }
    }
    • topLevel — this is our main root in application (You can change the name you want. For example: news),
    • pages — define here your pages.
      helloworld — this is variable for page, we will using this in the next tutorials,
    • friendly — friendly URL for us. If you leave empty your URL will be ` http://localhost/ipsdev/helloworld` (or ` http://localhost/ipsdev/index.php?/helloworld/ ` if you not remove index.php in configuration IPS engine)
    • real — real URL for our page

    You can define more page. Here is the example:

    {
      "topLevel": "helloworld",
      "pages": {
        "helloworld": {
          "friendly": "",
          "real": "app=helloworld&module=helloworld&controller=pagetest"
        },
        "test": {
          "friendly": "test",
          "real": "app=helloworld&module=helloworld&controller=test"
        }
      }
    }

    In this code we have:

    Bonus: Add page to menu

    To add page to the menu we have to create extension for our app. Go to AdminCP and go to “Developer Center” our app and open “Extensions” tab.

    1_1FOZM_HbQoBSVf_2_7W8kw.webp.a3aaf7bf3619f51228239493171d5aa9.webp

    Find “FrontNavigation” extension and click “Create FrontNavigation Extension” button.

    1_ZnN2hznbui9flRX266t1Fg.webp.9e8573b4d4ab512750ac6b0c019a183b.webp
     

    Type a classname. I’ll write “helloworld”.

    In our core automatically create `helloworld.php` file in `extensions/core /FrontNavigation`. In this file you can menage your page. I change only link method and add lang key into `dev/lang.php`.

    /**
      * Get Link
      *
      * @return \IPS\Http\Url
      */
     public function link()
     {
      return \IPS\Http\Url::internal("app=helloworld&module=helloworld&controller=pagetest");
     }

    In `dev/lang.php` I’ll add “frontnavigation_helloworld”:

    <?php
    
    $lang = [
     
     "frontnavigation_helloworld" => "Hello World"
    ];

    Now you can add page into your menu. Go to AdminCP => SITE FEATURES => Menu Manager and click “Create Item”. Click to your page and add this!

    1_9ilWODhM1KEV8wOLXtB-mQ.webp.40dd5379dd46a01c67018ebc26df5dbd.webp

    When you added page to menu click “Publish this menu”.

    1_Viu4I7FzTD-5TRHo50b0ow.webp.8d2795c5f850af1b286628b01eed3c1a.webp
     

    Congrats! You have page form our app in your menu!

    Create JavaScript file

    You can create also JavaScript file. It’s the same flow like create CSS file. Go to files your application and open `dev/js` folder. You have to create a location folder.

    We have 3 primary folders here (exactly like HTML from previous tutorial and CSS):

    • front — Display in front,
    • admin — Display in AdminCP,
    • global — Display in front and in AdminCP

    JavaScript file are grouping so we have to create new folder for our group.

    From IPS docs:

    Цитата

    Javascript files are bundled by location and group, and that forms the filename of the resulting bundle

    For example, if we created javascript files in `dev/js/front/controllers/mygroup/somefile.js`, then the bundle name would be front_mygroup.js (location, underscore, groupname), and loaded as follows:

    \IPS\Output::i()->jsFiles = array_merge( \IPS\Output::i()->jsFiles, \IPS\Output::i()->js( 'front_mygroup.js', 'app', 'location' ) );

    For more information you can check here in documentation.

    In our app I create`dev/js/front/controllers/pagetest/pagetest.js`.

    Inside `pagetest.js`I create code with console log for testing our page.

    1_0ZqmJPyQZTmhvk_n9dez8A.webp.3e5a738415f3076e15d9625a14aac2af.webp

    console.log('test for pagetest');

    Import JavaScript file into controller

    Now we have to import JavaScript file to our controller. Go to `modules/front/helloworld/pagetest.php` In execute method import our JavaScript file like this:

    \IPS\Output::i()->jsFiles = array_merge(\IPS\Output::i()->jsFiles, \IPS\Output::i()->js('front_pagetest.js', 'helloworld', 'front'));

    Now check our page http://localhost/ipsdev/helloworld/ and open console in the browser (F12).

    1_Bo7UGguDdtCZQRgbe8pOCg.thumb.webp.ad9c234e938eec3284156f9e43e440cc.webp
     

    It’s working! You can create JavaScript code whatever you want.

    Images and Other Resources

    You can upload images and other resources to your app. In this tutorial I upload logo into our app. Go to `dev/resources`. You have to create a location folder.

    We have 3 primary folders here (exactly like HTML from previous tutorial and CSS, JavaScript):

    • front — Display in front,
    • admin — Display in AdminCP,
    • global — Display in front and in AdminCP

    Now put your file into module folder. For example into front folder.

    1_16zFL7339x4KBmGl356CcQ.webp.44c60d2abf112e97e84a1733c655f359.webp

    Now we can put image into HTML file. Go to `dev/html/front/index.phtml` and add image into `img` tag.

    <img src='{resource="logo_dark.png" app="helloworld" location="front"}' alt='Logo aXenDev'>

    Full code:

    <ips:template parameters="" />
    <h1 class="ipsType_pageTitle ipsFlex-flex:11 ipsType_break ipsMargin_bottom title">
      {lang="hello_world_test_page"}
    </h1>
    
    <div class='ipsBox ipsPadding'>
      <div class='ipsMessage ipsMessage_error'>
        <span class='ipsMessage_code'>12345</span>
        This is an error message.
      </div>
    
      <img src='{resource="logo_dark.png" app="helloworld" location="front"}' alt='Logo aXenDev'>
    
      Created by aXenDev!
    </div>

    Now check our page http://localhost/ipsdev/helloworld/.

    1_9S__Edi2_zYcmChNmBSHwA.webp.fcea57f6264cf969a8ff48d01a5a436e.webp

    Congratulations! You have a image in your app!

    If you want to read more about resources you can check the documentation.

    Credit to Maciej Piotr Balcerzak


    Обратная связь

    Рекомендуемые комментарии

    Комментариев нет



    Присоединяйтесь к обсуждению

    Вы можете написать сейчас и зарегистрироваться позже. Если у вас есть аккаунт, авторизуйтесь, чтобы опубликовать от имени своего аккаунта.
    Примечание: Ваш пост будет проверен модератором, прежде чем станет видимым.

    Гость
    Добавить комментарий...

    ×   Вставлено с форматированием.   Вставить как обычный текст

      Разрешено использовать не более 75 эмодзи.

    ×   Ваша ссылка была автоматически встроена.   Отображать как обычную ссылку

    ×   Ваш предыдущий контент был восстановлен.   Очистить редактор

    ×   Вы не можете вставлять изображения напрямую. Загружайте или вставляйте изображения по ссылке.


×
×
  • Создать...